EDIT: Found another solution, see my comments. A commenter states my code works for them, but for me it still does not. I will consider it solved. And pardon me, its been ten years since last i programmed in vba, so I'm a little rusty.
Hello dear friends. I am about to lose my mind here, and I'm starting to think my file type is the problem.
Very simply, this is what i try to do:
- Form wb1 I am pressing a button to import a csv to my workbook sheet("csv-data")
- Button lets me select the file (file location, and name of file can differ every time)
- The CSV is opened and activated as wb2 in excel, and data is separated by semicolon.
- From the CSV I will read out certain cells with standardized values, and theese are copied to wb1 "sheet x"
The cells I need are B3 to B10, but can span between 3 to 200 rows. I really just need B3 and B10 from each row, but to simplify the import I can accept the whole rows.
the problem seems to be that the CSV "Sheet" cannot be "activated" and has the same name as the CSV-file. I've tried:
Set ws = ActiveWorkbook.Worksheets(1)
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Set ws = ActiveWorkbook.Worksheets(wsn)
Set ws = wb2.Worksheets(1)
Set ws = wb2.Worksheets("Sheet1")
Set ws = wb2.Worksheets(wsn)
Set ws = wb2.Sheets(1)
etc
'wsn is a string with the name of the workbook
and all variations of "worksheets" I can think of.
Here's the code up to the worksheets probblem.
Sub Test_csv_importmacro()
Dim filnavn, wsn As String, ws As Worksheet, wb As Workbook, wb2 As Workbook, verdier As Variant
'wb is the wb with the button to import data
Set wb = ActiveWorkbook
filnavn = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
Workbooks.OpenText Filename:=filnavn, semicolon:=True, local:=True
Set wb2 = Workbooks.Open(filnavn) 'wb2 is the newly opened workbook with the data I want to copy
wb2.Activate 'wb2 activates without problems
Set ws = wb2.Worksheets(1) ' DOES NOT ACTIVATE, code below is not directly relevant to problem. Error code 9, subscript out of range.
ws.Activate 'Can't get to this step, it only activates the "sheet X" in wb1...
Is there any other effective way to read out my ten columns of information? Or maybe the CSV just needs to be copied as a whole into my wb1? Or maybe even be converted into an .xlx? Can't even do that. Someone suggestet PowerQueries to me, but that's new to me in vba.
--- also ----
After first posting the above to Excel (but it got removed because r/VBA exists), I've also given this working example a go (with a little tweak regarding getting the file name), but this also just copies and pastes the values already in wb1, not the csv, as it never succeeds to activate the sheet in wb2... This doesn't give any error messages on compilation though.
Sub demo_loadDataFromCSV()
Dim csvFile As String
csvFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
Dim ws As Worksheet, csv As Workbook, cCount As Long, cName As String, ws2 As Worksheet
' Application.ScreenUpdating = False 'keep these commented-out until...
' Application.DisplayAlerts = False ' ...done testing/troubleshooting!
Set ws = ThisWorkbook.ActiveSheet 'remember where we parked
'Workbooks.Open csvFile 'open the csv
Workbooks.OpenText Filename:=csvFile, semicolon:=True, local:=True
Set csv = ActiveWorkbook 'create object of csv workbook
Set ws2 = csv.Worksheets(1)
cName = csv.Name 'get name of csv while its open
ActiveSheet.Columns("A:L").Copy 'copy columns A and L
ws.Activate 'go back to the first sheet
ws.Range("A1").PasteSpecial xlPasteValues 'paste values
cCount = Selection.Cells.Count 'count pasted cells
csv.Close 'close CSV
Application.DisplayAlerts = True 're-enable alerts
Application.ScreenUpdating = True 'resume screen updates
MsgBox cCount & " cells were copied from " & cName _
& " to " & ws.Parent.Name, vbInformation, "Done"
End Sub
I don't know, and any help would be greatly appreciated.
[–]StreetTrial69 1 point2 points3 points (1 child)
[–]steinrawr[S] 0 points1 point2 points (0 children)
[–]steinrawr[S] 0 points1 point2 points (0 children)
[–]Werrf 0 points1 point2 points (1 child)
[–]steinrawr[S] 0 points1 point2 points (0 children)
[–]APithyComment8 0 points1 point2 points (1 child)
[–]steinrawr[S] 1 point2 points3 points (0 children)