issues after text import

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

<p>

I am able to import CSV files into 3 worksheets of a workbook without issues.
But when I tried to add code to make changes, It did not take the change.
I suspect the worksheet is not Active. So I tried to again set the workbook and worksheet
But it does not chnage the format or name the column BB as Date or make the other changes
</p>
<code>
Sub TSS()

Dim wbexcel As Workbook
Dim ws As Worksheet
Dim intTextFile As Integer
Dim strFileName As String
Dim LastRow As Long
Dim i As Long


Set wbexcel = ThisWorkbook
Set ws = wbexcel.Sheets("TSS Trans")
ws.Cells.ClearContents

'Import TSS Trans CSV
strFileName = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Provide Text or CSV File:")

With ws.QueryTables.Add(Connection:="TEXT;" & strFileName, Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh

End With

MsgBox ("TSS Trans imported")

Set wbexcel = ActiveWorkbook
Set ws = wbexcel.Sheets("TSS Trans")

'change the format to text
ws.Columns("AG:AG").NumberFormat = "@"

'Add Date column

ws.Range("BB1").Name = "Date"

LastRow = Range("AY" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("AY" & i).Value = " " Or Range("AY" & i).Value = Empty Then
Range("BB" & i).Value = "1"
End If
Next i
End Sub
</code>
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I was able to resolve it by including the worksheet reference as in ws.Range rather than just If Range("AY" & i).Value = " " Or Range("AY" & i).Value = Empty Then

LastRow = ws.Range("AY" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If ws.Range("AY" & i).Value = " " Or ws.Range("AY" & i).Value = Empty Then
ws.Range("BB" & i).Value = "1"
End If
Next i
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top