SurreyNick
Member
- Local time
- Today, 01:54
- Joined
- Feb 12, 2020
- Messages
- 127
I have this bit of code which opens an existing Excel workbook from a file picker in one of my forms (txtFileName) and which then deletes all but one sheet (which is renamed) and then saves the workbook with the new name "Target.xlsx". Ultimately this will be imported into my db.
The problem is I think my code is crap. I have spent several days scouring online resources and trying various approaches to come up with something I can make work, but I have a strong suspicion I have gone about this the wrong way. The code I have here works in the first instance I run it but if I run it again without closing and reopening the db I get the error "Object variable or With block variable not set" and I can't work out how to resolve it.
My routine is going to be quite lengthy because I have a lot of data manipulation to do on the worksheet before importing it into my db and I want to get the right code and get it right at the outset before going any further.
With this in mind I would really appreciate some assistance here; (1) to tell me if there is a better approach I should adopt for this task, and (2) where the fault is in my code.
Please use code tags. ..
The problem is I think my code is crap. I have spent several days scouring online resources and trying various approaches to come up with something I can make work, but I have a strong suspicion I have gone about this the wrong way. The code I have here works in the first instance I run it but if I run it again without closing and reopening the db I get the error "Object variable or With block variable not set" and I can't work out how to resolve it.
My routine is going to be quite lengthy because I have a lot of data manipulation to do on the worksheet before importing it into my db and I want to get the right code and get it right at the outset before going any further.
With this in mind I would really appreciate some assistance here; (1) to tell me if there is a better approach I should adopt for this task, and (2) where the fault is in my code.
Please use code tags. ..
Code:
Private Sub btnRunMacro_Click()
Dim mySheetNames() As Variant
mySheetNames = Array("Topic & Skills Breakdown", "Statements and Marks", "Generated Feedback")
Call OpenExcelFile(Me.txtFileName) [COLOR=rgb(65, 168, 95)]'<txtFileName is a text box file picker on my form[/COLOR]
With ActiveWorkbook
.SaveAs fileName:="C:\Users\Nick\Desktop\Target.xlsx"
.Application.DisplayAlerts = False
.Sheets(mySheetNames).Delete
.Application.DisplayAlerts = True
.Worksheets("Mark Sheet").Name = "Sheet1"
[COLOR=rgb(65, 168, 95)] 'add additional code to manipulate data[/COLOR]
.Close SaveChanges:=True
End With
End Sub
Last edited by a moderator: