Hi,
I'm "alright" with VBA at this point for someone only really working in it for a couple of months. I'm stuck on something that seems harder than it may be and was wondering what I'm doing wrong.
I'm looking to take a workbook (DATS), transpose data from specific cells to a new sheet and name the new sheet the value of a cell. From there, save that new sheet as a separate workbook to a new file in a different folder (on a different drive), check to see if that file exists - if it does, ask to overwrite and if that is okay, overwrite. if not, stop. For both delete the new sheet and notify the user the data has or has not been saved depending.
My existing patchwork code is below.
This is a stepped process (i.e. I'll work on bits at a time. Took me two weeks to get this far...
Right now, I run to an error 1004 at:
Advice would be greatly appreciated and thank you in advance.
I'm "alright" with VBA at this point for someone only really working in it for a couple of months. I'm stuck on something that seems harder than it may be and was wondering what I'm doing wrong.
I'm looking to take a workbook (DATS), transpose data from specific cells to a new sheet and name the new sheet the value of a cell. From there, save that new sheet as a separate workbook to a new file in a different folder (on a different drive), check to see if that file exists - if it does, ask to overwrite and if that is okay, overwrite. if not, stop. For both delete the new sheet and notify the user the data has or has not been saved depending.
My existing patchwork code is below.
Code:
Sub Cloud()
'=============================================================================================================
'This macro asks the suer if they are connected to the VPN. If they are, the sheet will transfer data to a new
'sheet and name it based on the data in cell AM3. AM3 is concatenated L2,AJ2, AJ3, and E5. This lets each sheet
'uploaded have a name unique to the auditor, but still classified by division, month, and week of month.
'
'If the user attempts to create a dataset a week in advance, they are denied.
'If the user attempts to create a second set of data in the same week without using a fresh sheet, they are denied
'
'If the user is not connected to the VPN, macro ends without doing anything and the user is asked to connect
'before attempting again.
'=============================================================================================================
Dim ws As Worksheet, chkShtName As String
'VPN connection question
MSG1 = MsgBox("Are you currently connected to the VPN?", vbYesNo + vbQuestion, "VPN CONNECTION CHECK")
'Set variable to value in DATS!AM3
chkShtName = Sheets("DATS").Range("AM3")
If MSG1 = vbYes Then
MsgBox "Please remain connected until this task is complete."
'If chkShtName is empty, notify and exit
'chkShtName should never be empty it pulls from concatenated data in cell AM3, but data can be incomplete and handled later.
If chkShtName = "" Then
MsgBox "CRITICAL ERROR: AM3 DATA MISSING" & vbNewLine & vbNewLine & _
"Please use the 'Help Ticket' function in the upper right of the DATS form and inform of an 'AM3 Data Missing' error."
Exit Sub
End If
'If chkShtName (data in AM3) is not empty, check to see if Sheet exists.
'chShtName is concatenated information from 4 cells.
'The Set instruction will produce an error if the sheet does not exist.
'If sheet doesn't exist, it is "nothing"
On Error Resume Next
Set ws = Sheets(chkShtName)
On Error GoTo 0
'If ws is Not Nothing then it exists and tell the user, exit sub
If Not ws Is Nothing Then
MsgBox "You can't create the same week of data twice in the same Workbook."
Exit Sub
End If
'add sheet named from data in cell AM3
ActiveSheet.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = chkShtName
'delete direct copy and reset the entire sheet to default
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Cells.Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveSheet.Shapes.Range(Array("Picture 4")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Picture 2")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Picture 1")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.Delete
'clear copied formatting and reset the text, color, etc.
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Selection.ClearContents
'label headers
ActiveCell.FormulaR1C1 = "Auditor"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Date"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Store"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Arrive"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Start"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Stop"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Depart"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Drive"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Research"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Audit"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Close"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Retail"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Speed"
'format cells
Range("B2:B7").Select
Selection.NumberFormat = "m/d/yy;@"
Range("D2:G7").Select
Selection.NumberFormat = "[$-409]h:mm AM/PM;@"
ActiveCell.FormulaR1C1 = "=DATS!R[3]C[4]"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=IF(DATS!R5C5="""","""",DATS!R5C5)"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A7"), Type:=xlFillDefault
Range("A2:A7").Select
Range("B2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[32]C[2]"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B7"), Type:=xlFillDefault
'transpose data
Range("C2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[23]C[1]"
Range("C3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[22]C[3]"
Range("C4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[21]C[5]"
Range("C5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[20]C[7]"
Range("C6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[19]C[9]"
Range("C7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[18]C[11]"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[13]C"
Range("D3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[12]C[2]"
Range("D4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[11]C[4]"
Range("D5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[10]C[6]"
Range("D6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[9]C[8]"
Range("D7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[8]C[10]"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[26]C[-1]"
Range("E3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[25]C[1]"
Range("E4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[24]C[3]"
Range("E5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[23]C[3]"
Range("E5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[23]C[5]"
Range("E6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[22]C[7]"
Range("E7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[21]C[9]"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[26]C[-1]"
Range("F3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[25]C[1]"
Range("F4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[24]C[3]"
Range("F5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[23]C[5]"
Range("F6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[22]C[7]"
Range("F7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[21]C[9]"
Range("G2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[12]C[-2]"
Range("G3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[11]C"
Range("G4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[10]C[2]"
Range("G5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[9]C[4]"
Range("G6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[8]C[6]"
Range("G7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[7]C[8]"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[16]C[-4]+DATS!R[16]C[-3]"
Range("H3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[15]C[-2]+DATS!R[15]C[-1]"
Range("H4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[14]C+DATS!R[14]C[1]"
Range("H5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[13]C[2]+DATS!R[13]C[3]"
Range("H6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[12]C[4]+DATS!R[12]C[5]"
Range("H7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[11]C[6]+DATS!R[11]C[7]"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=(RC[-4]-RC[-5])*24"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I7"), Type:=xlFillDefault
Range("I2:I7").Select
Selection.AutoFill Destination:=Range("I2:K7"), Type:=xlFillDefault
Range("I2:K7").Select
Range("L2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[24]C[-8]"
Range("L3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[23]C[-6]"
Range("L4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[22]C[-4]"
Range("L5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[20]C[-2]"
Range("L6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[20]C"
Range("L5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[21]C[-4]"
Range("L5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[21]C[-2]"
Range("L6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[20]C"
Range("L7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[19]C[2]"
Range("M2").Select
ActiveCell.FormulaR1C1 = "=DATS!R[22]C[-8]"
Range("M3").Select
ActiveCell.FormulaR1C1 = "=DATS!R[21]C[-6]"
Range("M4").Select
ActiveCell.FormulaR1C1 = "=DATS!R[20]C[-4]"
Range("M5").Select
ActiveCell.FormulaR1C1 = "=DATS!R[19]C[-2]"
Range("M6").Select
ActiveCell.FormulaR1C1 = "=DATS!R[18]C"
Range("M7").Select
ActiveCell.FormulaR1C1 = "=DATS!R[17]C[2]"
'format cell size to accomodate the data
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Application.ScreenUpdating = True
Application.DisplayAlerts = True
ActiveSheet.Copy
'make new workbook from new sheet
With ActiveSheet.UsedRange
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
ChDir "C:\Users\xxxxx\Desktop"
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\Users\xxxxx\Desktop\" & chkShtName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Else
MsgBox "Connecting to the VPN is critical to this task." & vbNewLine & _
vbNewLine & "Please connect and try again or save this sheet and upload your data when you're able to connect." & vbNewLine & _
vbNewLine & "If you're unable to connect for an extended period because of network issues or travel," & _
" please notify your Division Manager and Senior Auditors."
End If
End Sub
This is a stepped process (i.e. I'll work on bits at a time. Took me two weeks to get this far...
Right now, I run to an error 1004 at:
Code:
With ActiveSheet.UsedRange
.PasteSpecial xlValues
.PasteSpecial xlFormats
Advice would be greatly appreciated and thank you in advance.