For work, I have to pull various information from a USDA report. I have created a sub that will pull the information I need from a text file, into a worksheet "USDA Weekly." I created another sub, using the recorder, that uses text to column to split the information (via fixed width). The sub that does the splitting is the only sub that changes the information on the "USDA Weekly" sheet in any way. All other subs pull information from this sheet.
I have a button on another worksheet (in the same workbook) that runs all the subs I have created for the report I have to do, including the two subs mentioned. Now, when I run sub to split the information it works perfectly however when I click the button to run all the subs including the splitter it doesn't work.
I have run this in the debugger step by step numerous times, in an attempt to figure out why this is happening with no luck. I'm mostly confused as to why it the sub doesn't work when the button is clicked, yet works when run by itself. Any tips as to why this isn't working is appreciated.
Edit: clarification on how this doesn't work. When the splitting sub is run through the button, it does not split the column at all, as shown below. No errors, or anything pop-up.
Edit Edit: as per Mathieu Guindon (implict) suggestion I have modified formatUSDAWeekly to use a with statement to avoid implicit references.
Sub formatUSDAWeekly()
'this sub pulls information from strictly within the workbook
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook 'sets wb to the workbook that contains the code (i.e. this workbook)
Set ws = wb.Sheets("USDA Weekly")
With ws
.Range("A:A").TextToColumns Destination:=.Range("A:A"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(39, 1), Array(50, 1), Array(52, 1), Array(61, 1), _
Array(73, 1)), TrailingMinusNumbers:=True
End With
End Sub
This is the sub for the button
Sub start()
Call pullFrom610
Call formatUSDAWeekly
Call formatWIWorkbook
Call formatOSWorkbook
End Sub
This is the sub that pulls the information
Sub pullFrom610()
'this code was taken from Seamus Abshere
'on SO:https://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("USDA Weekly")
ws.Columns("A:F").ClearContents 'clears the previous information
With ws.QueryTables.Add(Connection:="URL;https://www.ams.usda.gov/mnreports/lm_pk610.txt", Destination:=ws.Range("A1"))
.RefreshStyle = xlOverwriteCells
.SaveData = True
.Refresh
End With
End Sub
This is the sub that splits the information using text to column
Sub formatUSDAWeekly()
'this sub pulls information from strictly within the workbook
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook 'sets wb to the workbook that contains the code (i.e. this workbook)
Set ws = wb.Sheets("USDA Weekly")
ws.Range("A:A").TextToColumns Destination:=Range("A:A"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(39, 1), Array(50, 1), Array(52, 1), Array(61, 1), _
Array(73, 1)), TrailingMinusNumbers:=True
End Sub