Want2BExcel
Board Regular
- Joined
- Nov 24, 2021
- Messages
- 114
- Office Version
- 2016
- Platform
- Windows
I recorded this macro to get data from a csv file and I want to make it more user-friendly. So I'm looking for some assistance to a few things.
In this example I'm choosing a specific file D:\Økonomi\Budget\Budget_JAN_22.csv but I want fileexplore to open and let the user point to the location of the wanted csv file. Futhermore I can see in the code that the name of the csv file in my example Budget_JAN_22 is used multible times. These sections need to be depending of the chosen file, so if the file is called Budget_MAY_23.csv then that's the name used throughout the procedure.
In the end of the recorded code, I have another issue, well I have two.
1. Renaming and moving the sheet. I see a problem since the sheet number varies from user to user (here it's sheet5 [Ark5]), so how do I solve both moving and renaming a sheet that can have various numbers?
Renaming is important because another macro should be able to find it later on. I piture this solution to be a pop-up window where the user input the sheet name, maybe with some restrictions to three letter month name, a space, an ', and to number year. In my example: Jan '22 (if possible). If this is to much trouble, then nevermind. I'll just explain the importence of this somehow. But if it's possible it would be mindblowing fantastic!!!!
2. To make the whole workbook look nice, I move the sheet Jan '22 to the right place (in the code it's before sheet 91) after Dec '21 The new sheets will always be placed month/year after another. So next will be Feb '22, Mar '22 etc. Can this also be automated?
YES I have a lot of sheets ? but I've been building/using this workbook since 2015. Now I just want to share it to all that wants it and for that I need to make something more efficient and user-friendly, because there is alot going on....and very few people love playing with Excel as much as I do ?
In this example I'm choosing a specific file D:\Økonomi\Budget\Budget_JAN_22.csv but I want fileexplore to open and let the user point to the location of the wanted csv file. Futhermore I can see in the code that the name of the csv file in my example Budget_JAN_22 is used multible times. These sections need to be depending of the chosen file, so if the file is called Budget_MAY_23.csv then that's the name used throughout the procedure.
In the end of the recorded code, I have another issue, well I have two.
1. Renaming and moving the sheet. I see a problem since the sheet number varies from user to user (here it's sheet5 [Ark5]), so how do I solve both moving and renaming a sheet that can have various numbers?
Renaming is important because another macro should be able to find it later on. I piture this solution to be a pop-up window where the user input the sheet name, maybe with some restrictions to three letter month name, a space, an ', and to number year. In my example: Jan '22 (if possible). If this is to much trouble, then nevermind. I'll just explain the importence of this somehow. But if it's possible it would be mindblowing fantastic!!!!
2. To make the whole workbook look nice, I move the sheet Jan '22 to the right place (in the code it's before sheet 91) after Dec '21 The new sheets will always be placed month/year after another. So next will be Feb '22, Mar '22 etc. Can this also be automated?
YES I have a lot of sheets ? but I've been building/using this workbook since 2015. Now I just want to share it to all that wants it and for that I need to make something more efficient and user-friendly, because there is alot going on....and very few people love playing with Excel as much as I do ?
VBA Code:
Sub HentDATAfraCSV2()
'
' HentDATAfraCSV2 Makro
'
'
ActiveWorkbook.Queries.Add Name:="Budget_JAN_22", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Kilde = Csv.Document(File.Contents(""D:\Økonomi\Budget\Budget_JAN_22.csv""),[Delimiter="";"", Columns=4, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Hævede overskrifter"" = Table.PromoteHeaders(Kilde, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Ændret type"" = Table.TransformColumnTypes(#""Hævede overskrifter"",{{""Dato"", type date}, {""Tekst"", type text}" & _
", {""Beløb"", type number}, {""Saldo"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Ændret type"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Budget_JAN_22;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Budget_JAN_22]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Budget_JAN_22"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheets("Ark5").Select
Sheets("Ark5").Name = "Jan '22"
Sheets("Ark5").Select
Sheets("Ark5").Move Before:=Sheets(91)
End Sub