I'm don't know VBA and I'm only doing this out of necessity so forgive me. I normally just do T-SQL.
I found the following code in a help article to take data from an Excel sheet and output it into a txt file which will be used to run through Scribe. So far;
- I've placed the Command button on the sheet
- I've right clicked to View Code
- I pasted the code from the article into the VBA editor and adapted parts of it for my needs (the file path basically)
When I run the code from the editor it performs perfectly.
When I close the editor and click the button on the sheet it runs but the resulting text file is just the appropriate number of empty strings in the text file. It's like it can see the number of rows that have data but not the actual data in the cells.
Have I missed something obvious? I can't see anything more in the help article (which isn't really written for complete novices I have to say) that would solve the problem!
Sub CommandButton1_Click()
Dim FilePath As String
Dim rng As Range
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Set rng = Worksheets("Discount Template").Range("B3")
FilePath = "\\SERVER2012\IT Dept\AccessSupplyChain\Product Discount Uploads\" & rng.Value & "_" & Format(Now(), "yyyymmdd hhmmss") & ".txt"
Open FilePath For Output As #2
For i = 1 To LastRow
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveCell(i, j).Value)
Else
CellData = CellData + Trim(ActiveCell(i, j).Value) + ","
End If
Next j
Print #2, CellData
CellData = ""
Next i
Close #2
MsgBox ("Done")
End Sub
ActiveSheetto find theLastColetc. but then references"Discount Template"thereafter. If the button isn't on the same sheet as the data, it won't see the data using this method.Sub CommandButton1_Click()see if it's being calledSet rng = Worksheets("Discount Template").Range("B3")as far as I can make out is only being used to get the customer reference number from the first sheet which is called "Discount Template." That variable isn't then being used in any other part of the script because that was something I took from another solution. The rng variable isn't part of the help article my question mentioned. I've commented that line of code out and remove it's use from the file path but that hasn't changed anything.Sub CommandButton1_Click()point of the script, which is the very beginning? When I click the button it just jumps into the editor and that row of code at the top is highlighted.