I am updating this post with update code.
Sub ImportWordTable()
Dim WordApp As Object
Dim WordDoc As Object
Dim arrFileList As Variant, FileName As Variant
Dim tableNo As Integer 'table number in Word
Dim tableStart As Integer
Dim tableTot As Integer
Dim LastRow As Long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
'On Error Resume Next
arrFileList = Application.GetOpenFilename("Word files (.doc; .docx),.doc;.docx", 2, _
"Browse For file containing table To be imported", , True)
If Not IsArray(arrFileList) Then Exit Sub '(user cancelled import file browser)
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
'Range("A:AZ").ClearContents
For Each FileName In arrFileList
Set WordDoc = WordApp.Documents.Open(FileName, ReadOnly:=True)
With WordDoc
tableNo = WordDoc.tables.Count
tableTot = WordDoc.tables.Count
If tableNo = 0 Then
MsgBox WordDoc.Name & " contains no tables", vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox(WordDoc.Name & " contains " & tableNo & " tables." & vbCrLf & _
"Enter the table To start from", "Import Word Table", "1")
End If
' Set the header for the Colums
With .tables(tableNo)
Parent.Range("A" & LastRow).Value = .cell(1, 1).Range
Parent.Range("B" & LastRow).Value = .cell(2, 1).Range
Parent.Range("C" & LastRow).Value = .cell(3, 1).Range
Parent.Range("D" & LastRow).Value = .cell(4, 1).Range
Parent.Range("E" & LastRow).Value = .cell(5, 1).Range
Parent.Range("F" & LastRow).Value = .cell(6, 1).Range
Parent.Range("G" & LastRow).Value = .cell(7, 1).Range
Parent.Range("H" & LastRow).Value = .cell(8, 1).Range
Parent.Range("I" & LastRow).Value = .cell(9, 1).Range
Parent.Range("J" & LastRow).Value = .cell(10, 1).Range
Parent.Range("K" & LastRow).Value = .cell(11, 1).Range
Parent.Range("L" & LastRow).Value = .cell(12, 1).Range
Parent.Range("M" & LastRow).Value = .cell(13, 1).Range
Parent.Range("N" & LastRow).Value = .cell(14, 1).Range
Parent.Range("O" & LastRow).Value = .cell(15, 1).Range
Parent.Range("P" & LastRow).Value = .cell(16, 1).Range
Parent.Range("Q" & LastRow).Value = .cell(17, 1).Range
Parent.Range("R" & LastRow).Value = .cell(18, 1).Range
Parent.Range("S" & LastRow).Value = .cell(19, 1).Range
Parent.Range("T" & LastRow).Value = .cell(20, 1).Range
Parent.Range("U" & LastRow).Value = .cell(21, 1).Range
Parent.Range("V" & LastRow).Value = .cell(22, 1).Range
Parent.Range("W" & LastRow).Value = .cell(23, 1).Range
Parent.Range("X" & LastRow).Value = .cell(24, 1).Range
Parent.Range("Y" & LastRow).Value = .cell(25, 1).Range
End With
LastRow = LastRow + 1
' Get the date from the tables
For tableStart = tableNo To tableTot
With .tables(tableStart)
.cell(1, 2).Range.Copy
With Range("A" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(2, 2).Range.Copy
With Range("B" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(3, 2).Range.Copy
With Range("C" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(4, 2).Range.Copy
With Range("D" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(5, 2).Range.Copy
With Range("E" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(6, 2).Range.Copy
With Range("F" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(7, 2).Range.Copy
With Range("G" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(8, 2).Range.Copy
With Range("H" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(9, 2).Range.Copy
With Range("I" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(10, 2).Range.Copy
With Range("J" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(11, 2).Range.Copy
With Range("K" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(12, 2).Range.Copy
With Range("L" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(13, 2).Range.Copy
With Range("M" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(14, 2).Range.Copy
With Range("N" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(15, 2).Range.Copy
With Range("O" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(16, 2).Range.Copy
With Range("P" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(17, 2).Range.Copy
With Range("Q" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(18, 2).Range.Copy
With Range("R" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(19, 2).Range.Copy
With Range("S" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(20, 2).Range.Copy
With Range("T" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(21, 2).Range.Copy
With Range("U" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(22, 2).Range.Copy
With Range("V" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(23, 2).Range.Copy
With Range("W" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(24, 2).Range.Copy
With Range("X" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.cell(25, 2).Range.Copy
With Range("Y" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
End With
LastRow = LastRow + 1
Next tableStart
.Close False
End With
Next FileName
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
It is working but having only one issue. if a cell is having multiple paragraph then it's getting pasted in multiple row of excel. I want like if 2nd column of 1st row of word file is having 3 paragraph, then in excel as well all these 3 paragraph should get pasted in same column.
Word Input
Excel File Output
Hope I am able to explain it properly.
I have amended your script to produce the desired results.
The clear contents and targeting is removed as this will allow other word tables to be added to the same sheet, amend the script if you want to clear the sheet each time.
The table start selection now works by setting the tableNo as the start of the loop
It pulls the heading from the first column of the first selected tableNo
VBA Script:
Sub ImportWordTable()
Dim WordApp As Object
Dim WordDoc As Object
Dim arrFileList As Variant, FileName As Variant
Dim tableNo As Integer 'table number in Word
Dim tableStart As Integer
Dim tableTot As Integer
Dim LastRow As Long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
'On Error Resume Next
arrFileList = Application.GetOpenFilename("Word files (*.doc; *.docx),*.doc;*.docx", 2, _
"Browse For file containing table To be imported", , True)
If Not IsArray(arrFileList) Then Exit Sub '(user cancelled import file browser)
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
'Range("A:AZ").ClearContents
For Each FileName In arrFileList
Set WordDoc = WordApp.Documents.Open(FileName, ReadOnly:=True)
With WordDoc
tableNo = WordDoc.tables.Count
tableTot = WordDoc.tables.Count
If tableNo = 0 Then
MsgBox WordDoc.Name & " contains no tables", vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox(WordDoc.Name & " contains " & tableNo & " tables." & vbCrLf & _
"Enter the table To start from", "Import Word Table", "1")
End If
' Set the header for the Colums
With .tables(tableNo)
Parent.Range("A" & LastRow).Value = .Cell(1, 1).Range
Parent.Range("B" & LastRow).Value = .Cell(2, 1).Range
Parent.Range("C" & LastRow).Value = .Cell(3, 1).Range
End With
LastRow = LastRow + 1
' Get the date from the tables
For tableStart = tableNo To tableTot
With .tables(tableStart)
.Cell(1, 2).Range.Copy
With Range("A" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.Cell(2, 2).Range.Copy
With Range("B" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
.Cell(3, 2).Range.Copy
With Range("C" & LastRow)
.Select
.Parent.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
.Borders.LineStyle = xlLineStyleNone
End With
End With
LastRow = LastRow + 1
Next tableStart
.Close False
End With
Next FileName
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
As I said in my above comment, one Word table column cannot copy directly, not having a range property. Two ways are recommended, to avoid copying of each table cell, which consumes Excel resources and is very slow. One way would be to select the respective column (VBA allows it) and copy/paste selection. But using clipboard it is slow for more involved documents.
The most efficient way is to place the column range in an array (working only in memory) and drop its content, at once, in the necessary range. Then, try to format it. Copying the format from Word is also slow and consumes resources. I used a trick: Transforming the dropped array content range in an listObject, then UnList it. To benefit of the standard table format. If you need them to be tables, you have to comment the code line tbl.UnList:
Sub ImportWordTable()
Dim WordApp As Object, WordDoc As Object, ws As Worksheet
Dim arrFileList As Variant, FileName As Variant, tableNo As Integer
Dim tableStart As Integer, tableTot As Integer, Target As Range
Set ws = ActiveSheet 'it is good to fully qualify all the objects
'so, use here the sheet you need
arrFileList = Application.GetOpenFilename("Word files (*.doc; *.docx),*.doc;*.docx", 2, _
"Browse for file containing table to be imported", , True)
If Not IsArray(arrFileList) Then Exit Sub '(user cancelled import file browser)
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
ws.Range("A:A").Clear 'clear its format, too...
Set Target = ws.Range("A1")
Dim i As Long, tbl As ListObject
For Each FileName In arrFileList
Set WordDoc = WordApp.Documents.Open(FileName, ReadOnly:=True)
With WordDoc
tableNo = WordDoc.tables.count
tableTot = WordDoc.tables.count
If tableNo = 0 Then
MsgBox WordDoc.name & " contains no tables", vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox(WordDoc.name & " contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
End If
For tableStart = tableNo To tableTot 'start iteration from `tableNo` if not the default...
Dim arr: ReDim arr(1 To .tables(tableStart).rows.count, 1 To 1) 'declare and ReDim the necessary aray
With .tables(tableStart)
For i = 1 To .rows.count 'load the array with the second column content
arr(i, 1) = .Range.Columns(2).cells(i)
arr(i, 1) = left(arr(i, 1), Len(arr(i, 1)) - 1) 'eliminate the Word strange ending line character
Next i
With Target.Resize(.rows.count, 1)
.Value = arr
Set tbl = ws.ListObjects.Add(xlSrcRange, ws.Range(.address), , xlYes) 'make the range a table
tbl.Unlist 'just transforming the table in range, but keeping its format...
End With
Set Target = Target.Offset(.Rows.Count + 2) 'reset Target
End With
Next tableStart
.Close False 'close the open document, without saving it
End With
Next FileName
WordApp.Quit
Set WordDoc = Nothing: Set WordApp = Nothing
End Sub
Related
I am trying to do find replace from excel to word using vba, but the problem is, in some of the word table it is keeping field blank.
After adjusting the table height in word it works but sometime it disturbs the other table and some time it paste as an image instead of text.
Below is the program which i have written for find and replace. Can anyone help me on below program. Thanks in advance.
Sub replication()
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim wd As Word.Application
Dim wdDoc As Word.Document
Dim irow As Long
Dim i As Long
Dim k As Long
Dim sh As Worksheet
Set wd = New Word.Application
Set sh = ThisWorkbook.Sheets("Sheet1")
irow = 3
i = Application.WorksheetFunction.CountA(Sheet1.Range("A2:IZ2").Value)
Do While sh.Range("A" & irow).Value <> ""
Set wdDoc = wd.Documents.Open(ThisWorkbook.Path & "\Standard.docx")
wd.Visible = False
On Error Resume Next
wdDoc.SaveAs2 (ThisWorkbook.Path & "\Word\" & sh.Range("B" & irow).Value & ".docx")
For j = 2 To 3
With wdDoc.Content.Find
.Text = Sheet1.Cells(2, j)
.Replacement.Text = Sheet1.Cells(irow, j)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next j
For k = 4 To i
With wdDoc.Content.Find
.Text = Sheet1.Cells(2, k)
If Len(Sheet1.Cells(irow, k)) > 120 Then
Sheet1.Cells(irow, k).Copy
'Selection.PasteExcelTable False, False, False
.Replacement.Text = "^c"
.Replacement.ClearFormatting
Else
.Replacement.Text = Sheet1.Cells(irow, k)
.Replacement.ClearFormatting
End If
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
'.Range.ParagraphFormat.Alignment = 3
End With
Next k
Dim footr As Word.HeaderFooter
For Each footr In wdDoc.Sections(1).Footers
With footr.Range.Find
.Text = "<Scheme Name>"
.Replacement.Text = Sheet1.Cells(irow, 2)
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With
Next footr
wd.Visible = False
Dim t As Table
'Windows(sh.Range("B" & irow).Value & ".docx").Activate
Documents(sh.Range("B" & irow).Value & ".docx").Activate
ActiveDocument.Range.Select
ActiveDocument.Range.Select
ActiveDocument.Range.Select
Documents(sh.Range("B" & irow).Value & ".docx").Activate
ActiveDocument.Range.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
wdDoc.ExportAsFixedFormat OutputFileName:= _
ThisWorkbook.Path & "\PDF\" & sh.Range("B" & irow).Value & ".pdf" _
, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=138, _
Item:=wdExportDocumentWithMarkup, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
wdDoc.Close
Set wdDoc = Nothing
irow = irow + 1
Loop
wd.Quit
Set wd = Nothing
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox "Replication done successfully!"
End Sub
I have a code that copy values from a selected sheet to a master sheet. When the first run is completed; I need to select the second sheet. For some strange reason I need to perform it 3 or more times before it is correct. I've checked it over and over but couldn't find it.
It are two codes but linked to each other.
Can somebody help me?
Sub Update_SISdata_STB()
'
' Update_SISdata Macro
'
Set Workbook = ThisWorkbook
Sheets("Meetstaten").Select
WorkbooknameSISdata = ActiveWorkbook.Name
MsgBox "Selecteer de steigerbouwdump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
Workbookname_ASESR = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If Workbookname_ASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
Else
Sheets("Meetstaten").Select
'Clear filter
On Error Resume Next
Sheets("Meetstaten").ShowAllData
Range("A6").Select
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
End With
If LastRow > 5 Then
Range("A6:V" & LastRow).Select
Selection.ClearContents
End If
Range("A6").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'WorkbooknameYMOE = ActiveWorkbook.Name
Workbooks.Open Filename:=Workbookname_ASESR
Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
'Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'laatste regel bepalen
End With
'copy Meetstaat, Project, Debiteur
Range("A2:C" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("C6").Select
ActiveSheet.Paste
'Workbooks.Open Filename:=Workbookname_ASESR
'Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
'Copy Prio1, prio2, prio3, prio4, prio5
Range("D2:H" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("F6").Select
ActiveSheet.Paste
'copy datum SES montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("M2:M" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("K6").Select
ActiveSheet.Paste
'copy datum SES huur, SESnr montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("P2:P" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("M6").Select
ActiveSheet.Paste
'copy SESnr Huur
' Windows(Workbookname_ASESR).Activate
' ActiveWindow.WindowState = xlMaximized
' Range("R2:R" & LastRow).Select
' Selection.Copy
'
' Windows(WorkbooknameSISdata).Activate
' Range("N6").Select
' ActiveSheet.Paste
'copy inhuur, uithuur
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("W2:X" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("O6").Select
ActiveSheet.Paste
'copy montage_demontage-bedrag, Huurbedrag
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AG2:AH" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("Q6").Select
ActiveSheet.Paste
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AK2:AN" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("S6").Select
ActiveSheet.Paste
Windows(Workbookname_ASESR).Close savechanges:=False
End If
Call Update_SISdata_ISO
Windows(WorkbooknameSISdata).Activate
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
End With
Range("A5:AM5").Select
Selection.AutoFilter
Range("A5:AM5").Select
Selection.AutoFilter
ActiveSheet.ShowAllData
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
ActiveSheet.Range("$A$5:$AM" & LastRow).AutoFilter Field:=25, Criteria1:="<=0", _
Operator:=xlAnd
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
With ActiveSheet
Set rngFilt = Application.Intersect(.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible), .Range("A:V"))
End With
rngFilt.Delete
ActiveSheet.ShowAllData
Range("W6:AM6").Select
Selection.AutoFill Destination:=Range("W6:AM1200"), Type:=xlFillDefault
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
For Edit_row = 6 To LastRow
Range("A" & Edit_row) = Mid(Range("D" & Edit_row), 34, 10)
If Range("M" & Edit_row) <> "CONFIRMED" Then
Range("B" & Edit_row) = Range("M" & Edit_row)
End If
If Range("K" & Edit_row).Value = " - -" Then
Range("K" & Edit_row) = ""
End If
If Range("L" & Edit_row).Value = " - -" Then
'If IsEmpty(Range("L" & Edit_row).Value) = True Then
Range("L" & Edit_row) = ""
End If
Next Edit_row
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
ActiveWorkbook.ActiveSheet.Range("S2") = Application.WorksheetFunction.Subtotal(109, Range("S6:S" & LastRow))
ActiveWorkbook.ActiveSheet.Range("S3") = Application.WorksheetFunction.Subtotal(109, Range("S6:S" & LastRow))
ActiveWorkbook.ActiveSheet.Range("T2") = Application.WorksheetFunction.Subtotal(109, Range("T6:T" & LastRow))
ActiveWorkbook.ActiveSheet.Range("T3") = Application.WorksheetFunction.Subtotal(109, Range("T6:T" & LastRow))
ActiveWorkbook.ActiveSheet.Range("U2") = Application.WorksheetFunction.Subtotal(109, Range("U6:U" & LastRow))
ActiveWorkbook.ActiveSheet.Range("U3") = Application.WorksheetFunction.Subtotal(109, Range("U6:U" & LastRow))
ActiveWorkbook.ActiveSheet.Range("V2") = Application.WorksheetFunction.Subtotal(109, Range("V6:V" & LastRow))
ActiveWorkbook.ActiveSheet.Range("V3") = Application.WorksheetFunction.Subtotal(109, Range("V6:V" & LastRow))
ActiveWorkbook.ActiveSheet.Range("W2") = Application.WorksheetFunction.Subtotal(109, Range("W6:W" & LastRow))
ActiveWorkbook.ActiveSheet.Range("W3") = Application.WorksheetFunction.Subtotal(109, Range("W6:W" & LastRow))
ActiveWorkbook.ActiveSheet.Range("X2") = Application.WorksheetFunction.Subtotal(109, Range("X6:X" & LastRow))
ActiveWorkbook.ActiveSheet.Range("X3") = Application.WorksheetFunction.Subtotal(109, Range("X6:X" & LastRow))
ActiveWorkbook.ActiveSheet.Range("Y2") = Application.WorksheetFunction.Subtotal(109, Range("Y6:Y" & LastRow))
ActiveWorkbook.ActiveSheet.Range("Y3") = Application.WorksheetFunction.Subtotal(109, Range("Y6:Y" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AF2") = Application.WorksheetFunction.Subtotal(109, Range("AF6:AF" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AG2") = Application.WorksheetFunction.Subtotal(109, Range("AG6:AG" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AH2") = Application.WorksheetFunction.Subtotal(109, Range("AH6:AH" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AI2") = Application.WorksheetFunction.Subtotal(109, Range("AI6:AI" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AJ2") = Application.WorksheetFunction.Subtotal(109, Range("AJ6:AJ" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AK2") = Application.WorksheetFunction.Subtotal(109, Range("AK6:AK" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AL2") = Application.WorksheetFunction.Subtotal(109, Range("AL6:AL" & LastRow))
' Verversen draaitabellen
Dim pivC As PivotCache
For Each pivC In ActiveWorkbook.PivotCaches
pivC.Refresh
Next
End Sub
Sub Update_SISdata_ISO()
'
' Update_SISdata Macro
'
Set Workbook = ThisWorkbook
Sheets("Meetstaten").Select
WorkbooknameSISdata = ActiveWorkbook.Name
MsgBox "Selecteer de Isolatiedump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
Workbookname_ASESR = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If Workbookname_ASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
Else
Sheets("Meetstaten").Select
'Clear filter
'Sheets("Meetstaten").ShowAllData
Range("A6").Select
Dim LastRow_STB As Long
With ActiveSheet
LastRow_STB = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
End With
' If LastRow_STB > 5 Then
' Range("A6:V" & LastRow).Select
' Selection.ClearContents
' End If
' Range("A6").Select
LastRow_STB = LastRow_STB + 1
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'WorkbooknameYMOE = ActiveWorkbook.Name
Workbooks.Open Filename:=Workbookname_ASESR
Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Dim LastRow_ISO As Long
With ActiveSheet
LastRow_ISO = .Cells(.Rows.Count, "A").End(xlUp).Row 'laatste regel bepalen
End With
'copy Meetstaat, Project, Debiteur
Range("A2:C" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("C" & LastRow_STB).Select
ActiveSheet.Paste
'Workbooks.Open Filename:=Workbookname_ASESR
'Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
'Copy Prio1, prio2, prio3, prio4, prio5
Range("F2:J" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("F" & LastRow_STB).Select
ActiveSheet.Paste
'copy datum SES montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AK2:AK" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("K" & LastRow_STB).Select
ActiveSheet.Paste
'copy datum SES huur blijft LEEG, SESnr montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AM2:AM" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("M" & LastRow_STB).Select
ActiveSheet.Paste
'copy SESnr Huur blijft LEEG
' Windows(Workbookname_ASESR).Activate
' ActiveWindow.WindowState = xlMaximized
' Range("R2:R" & LastRow).Select
' Selection.Copy
'
' Windows(WorkbooknameSISdata).Activate
' Range("N" & LastRow_STB).Select
' ActiveSheet.Paste
'copy inhuur, uithuur
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("P2:P" & LastRow_ISO).Select
Selection.Copy
' Inhuur
Windows(WorkbooknameSISdata).Activate
Range("O" & LastRow_STB).Select
ActiveSheet.Paste
' Uithuur
Windows(WorkbooknameSISdata).Activate
Range("P" & LastRow_STB).Select
ActiveSheet.Paste
'copy montage_demontage_bedrag, Huurbedrag
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("W2:W" & LastRow_ISO).Select
Selection.Copy
' Montage_demontage_bedrag
Windows(WorkbooknameSISdata).Activate
Range("Q" & LastRow_STB).Select
ActiveSheet.Paste
' Huurbedrag
' Windows(WorkbooknameSISdata).Activate
' Range("R" & LastRow_STB).Select
' ActiveSheet.Paste
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("W2:Z" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("S" & LastRow_STB).Select
ActiveSheet.Paste
Windows(Workbookname_ASESR).Close savechanges:=False
End If
End Sub
Try it without using select.
Option Explicit
Sub Update_SISdata_STB()
Dim wb As Workbook, ws As Worksheet
Dim LastRow As Long, Edit_row As Long
Dim rngFilt As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Meetstaten")
With ws
' clear filter
.AutoFilterMode = False
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
If LastRow > 5 Then
.Range("A6:V" & LastRow).ClearContents
End If
Range("A6").Select
End With
Call Import_SISdata_STB
Call Update_SISdata_ISO
With ws
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
' apply filter
.Range("$A$5:$AM" & LastRow).AutoFilter Field:=25, Criteria1:="<=0", Operator:=xlAnd
Set rngFilt = Application.Intersect(.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible), .Range("A:V"))
rngFilt.Delete
.AutoFilterMode = False
.Range("W6:AM6").AutoFill Destination:=.Range("W6:AM1200"), Type:=xlFillDefault
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
For Edit_row = 6 To LastRow
.Range("A" & Edit_row) = Mid(.Range("D" & Edit_row), 34, 10)
If .Range("M" & Edit_row) <> "CONFIRMED" Then
.Range("B" & Edit_row) = .Range("M" & Edit_row)
End If
If .Range("K" & Edit_row).Value = " - -" Then
.Range("K" & Edit_row) = ""
End If
If .Range("L" & Edit_row).Value = " - -" Then
'If IsEmpty(Range("L" & Edit_row).Value) = True Then
.Range("L" & Edit_row) = ""
End If
Next Edit_row
.Range("S2:Y3,AF2:AL2").Formula = "=Subtotal(109,S$6:S$" & LastRow & ")"
End With
' Verversen draaitabellen
Dim pivC As PivotCache
For Each pivC In ActiveWorkbook.PivotCaches
pivC.Refresh
Next
MsgBox "Done"
End Sub
Sub Import_SISdata_STB()
Dim wb As Workbook, wbIn As Workbook
Dim ws As Worksheet, wsIn As Worksheet
Dim FileASESR As Variant, LastRow As Long
'Select the scaffolding dump to import
MsgBox "Selecteer de steigerbouwdump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
FileASESR = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls), *.xls", _
Title:="Please select a file")
If FileASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
End If
Set wb = ThisWorkbook
Set ws = wb.Sheets("Meetstaten")
Set wbIn = Workbooks.Open(Filename:=FileASESR, ReadOnly:=True)
Set wsIn = wbIn.Sheets(1)
With wsIn
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'copy Meetstaat, Project, Debiteur
'copy Prio1, prio2, prio3, prio4, prio5
.Range("A2:H" & LastRow).Copy ws.Range("C6")
'copy datum SES montage
.Range("M2:M" & LastRow).Copy ws.Range("K6")
'copy datum SES huur, SESnr montage
.Range("P2:P" & LastRow).Copy ws.Range("M6")
'copy inhuur, uithuur
.Range("W2:X" & LastRow).Copy ws.Range("O6")
'copy montage_demontage-bedrag, Huurbedrag
.Range("AG2:AH" & LastRow).Copy ws.Range("Q6")
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
.Range("AK2:AN" & LastRow).Copy ws.Range("S6")
End With
MsgBox "Imported " & LastRow - 1 & " rows from " & wsIn.Name, vbInformation, wbIn.Name
wbIn.Close savechanges:=False
End Sub
Sub Update_SISdata_ISO()
' Update_SISdata Macro
Dim wb As Workbook, wbIn As Workbook
Dim ws As Worksheet, wsIn As Worksheet
Dim FileASESR As Variant
Dim LastRow_STB As Long, LastRow_ISO As Long
' Select the Insulation Dump to import
MsgBox "Selecteer de Isolatiedump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
FileASESR = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls), *.xls", _
Title:="Please select a file")
If FileASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
End If
Set wb = ThisWorkbook
Set ws = wb.Sheets("Meetstaten")
LastRow_STB = 1 + ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set wbIn = Workbooks.Open(Filename:=FileASESR, ReadOnly:=True)
Set wsIn = wbIn.Sheets(1)
With wsIn
LastRow_ISO = .Cells(.Rows.Count, "A").End(xlUp).Row 'laatste regel bepalen
'copy Meetstaat, Project, Debiteur
.Range("A2:C" & LastRow_ISO).Copy ws.Range("C" & LastRow_STB)
'Copy Prio1, prio2, prio3, prio4, prio5
.Range("F2:J" & LastRow_ISO).Copy ws.Range("F" & LastRow_STB)
'copy datum SES montage
.Range("AK2:AK" & LastRow_ISO).Copy ws.Range("K" & LastRow_STB)
'copy datum SES huur blijft LEEG, SESnr montage
.Range("AM2:AM" & LastRow_ISO).Copy ws.Range("M" & LastRow_STB)
'copy inhuur
.Range("P2:P" & LastRow_ISO).Copy ws.Range("O" & LastRow_STB)
' Uithuur
.Range("P2:P" & LastRow_ISO).Copy ws.Range("P" & LastRow_STB)
'copy montage_demontage_bedrag, Huurbedrag
.Range("W2:W" & LastRow_ISO).Copy ws.Range("Q" & LastRow_STB)
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
.Range("W2:Z" & LastRow_ISO).Copy ws.Range("S" & LastRow_STB)
End With
MsgBox "Imported " & LastRow_ISO - 1 & " rows from " & wsIn.Name, vbInformation, wbIn.Name
wbIn.Close savechanges:=False
End Sub
I have a question about a problem that I can't seem to solve.
I have some VBA-code that includes a picture in a cell:
fNameAndPath = UserForm1.ComboBox2.Value
Set img = Application.ActiveSheet.Shapes.AddPicture(fNameAndPath, False, True, 1, 1, 1, 1)
With img
.Left = ActiveSheet.Range("G" & Lastrow).Left
.Top = ActiveSheet.Range("G" & Lastrow).Top
.Width = ActiveSheet.Range("G" & Lastrow).Width
.Height = ActiveSheet.Range("G" & Lastrow).Height
.Placement = 1
End With
This code runs perfectly for myself. The pictures are beautifully inserted.
However, 20% of my colleagues who use the macro find their picture in column H instead of column G.
I cannot come up with a reason why this happens.
Someone who encountered a related issue?
Thank you
Sorry for the delay, hereby the whole code that is in this macro.
Sub CommandButton3_Click()
‘ check whether conditions are OK
If UserForm1.TextBox1.Value = "" Or UserForm1.TextBox2.Value = "" Or UserForm1.TextBox3.Value = "" Or UserForm1.ComboBox1.Value = "" Then
MsgBox ("")
Exit Sub
End If
‘Check whether a picture was attached
If UserForm1.ComboBox2.Value = "" Then
MsgBox ("")
Exit Sub
End If
Workbooks("").Sheets("").Unprotect ""
‘Find username of the user
Dim username As String
username = Environ("username")
‘define lastrow and write some data in the cells
Lastrow = Worksheets("Objets Inutiles").Range("A650000").End(xlUp).Row + 1
Worksheets("Objets Inutiles").Range("A" & Lastrow).Value = "=Now()"
Worksheets("Objets Inutiles").Range("A" & Lastrow).Select
Selection.Copy
Worksheets("Objets Inutiles").Range("A" & Lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Objets Inutiles").Range("B" & Lastrow).Value = ""
Worksheets("Objets Inutiles").Range("C" & Lastrow).Value = username
Worksheets("Objets Inutiles").Range("C" & Lastrow).Select
Selection.Copy
Worksheets("Objets Inutiles").Range("C" & Lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Objets Inutiles").Range("D" & Lastrow).Value = UserForm1.ComboBox1.Value
Worksheets("Objets Inutiles").Range("E" & Lastrow).Value = UserForm1.TextBox2.Value
Worksheets("Objets Inutiles").Range("F" & Lastrow).Value = UserForm1.TextBox3.Value
Worksheets("Objets Inutiles").Range("H" & Lastrow).Value = UserForm1.TextBox1.Value
Worksheets("Objets Inutiles").Range("I" & Lastrow).Value = ""
Worksheets("Objets Inutiles").Range("J" & Lastrow).Value = ""
fNameAndPath = UserForm1.ComboBox2.Value
Set img = Application.ActiveSheet.Shapes.AddPicture(fNameAndPath, False, True, 1, 1, 1, 1)
With img
'Resize Picture to fit in the range....
.Left = ActiveSheet.Range("G" & Lastrow).Left
.Top = ActiveSheet.Range("G" & Lastrow).Top
.Width = ActiveSheet.Range("G" & Lastrow).Width
.Height = ActiveSheet.Range("G" & Lastrow).Height
.Placement = 1
End With
‘send some spam around
Dim EmailApp As Outlook.Application
Dim Source As String
Set EmailApp = New Outlook.Application
Dim EmailItem As Outlook.MailItem
Set EmailItem = EmailApp.CreateItem(olMailItem)
EmailItem.To = ""
EmailItem.CC = ""
EmailItem.BCC = ""
EmailItem.Subject = ""
EmailItem.HTMLBody = ""
EmailItem.Send
Unload UserForm1
Workbooks("").Sheets("").Protect ""
Workbooks("").Save
MsgBox ("")
Exit Sub
Unload Me
Workbooks("").Sheets("").Protect "Sapore"
Workbooks("").Save
MsgBox ("")
End Sub
I have a Userform with the following code attached to the "OK" button. All the code works fine other than the last 4 lines. Full code associated to OK button shown below, code not working is:
Dim SheetName As String
SheetName = ActiveSheet.Name
Call SheetCleanup
Worksheets(SheetName).Activate
It is as if it completely ignores it. SheetCleanup is located in a Module, and my suspicions are that there is an issue going from a Userform to a Module? But I am unfamiliar with this.
Full code is here:
Private Sub CommandButtonOK_Click()
If ComboBoxTargetEvent.Value = "" Or ComboBoxDesigner.Value = "" Or ComboBoxSignoff.Value = "" Or ComboBoxCarArea.Value = "" Or ComboBoxOriginator.Value = "" Or TextBoxNumberOfJobs.Value = "" Or ComboBoxProjectTitle.Value = "" Then _
MsgBox "You must complete all fields", vbInformation
Else:
'Go to worksheet based on Car Area
Dim CarArea As String
CarArea = ComboBoxCarArea.Value
Worksheets(CarArea).Activate
'Enter Target Event Into Column A
Columns("A").Find("", Cells(Rows.Count, "A")).Value = ComboBoxTargetEvent.Value
'Enter Project Title into column B
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 1).Value = ComboBoxProjectTitle.Value
'Enter Designer name into column E
If _
ComboBoxDesigner.Value <> "Various" Then _
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 4).Value = ComboBoxDesigner.Value
'Enter Sign-off name into column F
If _
ComboBoxSignoff.Value <> "Various" Then _
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 5).Value = ComboBoxSignoff.Value
'Enter Originator name into column F
If _
ComboBoxOriginator.Value <> "Various" Then _
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 3).Value = ComboBoxOriginator.Value
'Enter Data Formula into columns H & I
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-2, 7).Select
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-2, 7).Copy
Selection.Offset(1, 0).PasteSpecial Paste:=xlPasteFormulas
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-2, 8).Select
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-2, 8).Copy
Selection.Offset(1, 0).PasteSpecial Paste:=xlPasteFormulas
'Enter temp values into C & G
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 2).Value = "ENTER DESCRIPTION HERE (CAPS LOCK ONLY)"
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 6).Value = "ENTER DATE"
'Enter "N" into Job Completed
Columns("A").Find("", Cells(Rows.Count, "A")).Offset(-1, 9).Value = "N"
'Enter Data Validation List for Target Event
Dim ws As Worksheet
Dim NumberOfJobs As Long
Dim LastUsedInAA As Long
Dim range9 As Range
Set ws = ThisWorkbook.Worksheets("VBA_Data")
Set range9 = ws.Range("a:a")
LastUsedInAA = Range("A" & Rows.Count).End(xlUp).Row
NumberOfJobs = TextBoxNumberOfJobs.Value
If NumberOfJobs <> 0 Then
Range("A" & LastUsedInAA).Select
Selection.Resize(Selection.Rows.Count + NumberOfJobs - 1, _
Selection.Columns.Count).Select
With Selection.Validation
.Delete 'delete previous validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & range9.Address
End With
End If
'Enter Data Validation List for Designer
Dim LastUsedInE As Long
Dim range1 As Range
Set ws = ThisWorkbook.Worksheets("VBA_Data")
Set range1 = ws.Range("c:c")
LastUsedInE = Range("E" & Rows.Count).End(xlUp).Row
NumberOfJobs = TextBoxNumberOfJobs.Value
If NumberOfJobs <> 0 Then
Range("E" & LastUsedInE).Select
Selection.Resize(Selection.Rows.Count + NumberOfJobs - 1, _
Selection.Columns.Count).Select
With Selection.Validation
.Delete 'delete previous validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & range1.Address
End With
End If
'Enter Data Validation List for Senior Designer
Dim LastUsedInF As Long
Dim range2 As Range
Set ws = ThisWorkbook.Worksheets("VBA_Data")
Set range2 = ws.Range("b:b")
LastUsedInF = Range("F" & Rows.Count).End(xlUp).Row
NumberOfJobs = TextBoxNumberOfJobs.Value
If NumberOfJobs <> 0 Then
Range("F" & LastUsedInF).Select
Selection.Resize(Selection.Rows.Count + NumberOfJobs - 1, _
Selection.Columns.Count).Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & range2.Address
End With
End If
'Enter Data Validation List for Originator
Dim LastUsedInD As Long
Dim range5 As Range
Set ws = ThisWorkbook.Worksheets("VBA_Data")
Set range5 = ws.Range("f:f")
LastUsedInD = Range("D" & Rows.Count).End(xlUp).Row
NumberOfJobs = TextBoxNumberOfJobs.Value
If NumberOfJobs <> 0 Then
Range("d" & LastUsedInD).Select
Selection.Resize(Selection.Rows.Count + NumberOfJobs - 1, _
Selection.Columns.Count).Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & range5.Address
End With
End If
'Enter Data Validation List for Job Completed
Dim LastUsedInJ As Long
Dim range3 As Range
Set ws = ThisWorkbook.Worksheets("VBA_Data")
Set range3 = ws.Range("d:d")
LastUsedInJ = Range("J" & Rows.Count).End(xlUp).Row
NumberOfJobs = TextBoxNumberOfJobs.Value
If NumberOfJobs <> 0 Then
Range("J" & LastUsedInJ).Select
Selection.Resize(Selection.Rows.Count + NumberOfJobs - 1, _
Selection.Columns.Count).Select
With Selection.Validation
.Delete 'delete previous validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & range3.Address
End With
End If
'Multiply rows for multiple jobs
Dim LastUsedInA As Long
LastUsedInA = Range("A" & Rows.Count).End(xlUp).Row
NumberOfJobs = TextBoxNumberOfJobs.Value
If NumberOfJobs <> 1 Then
Range("A" & LastUsedInA).Select
Selection.Resize(Selection.Rows.Count, _
Selection.Columns.Count + 10).Copy
Range("A" & LastUsedInA + 1).Select
Selection.Resize(Selection.Rows.Count + NumberOfJobs - 2, _
Selection.Columns.Count).Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormulas
End If
'Clear the clipboard
Application.CutCopyMode = False
'select last cell in A
Range("A" & LastUsedInA).Select
'Clear all fields before hide
ComboBoxTargetEvent.Value = ""
ComboBoxDesigner.Value = ""
ComboBoxSignoff.Value = ""
ComboBoxCarArea.Value = ""
ComboBoxOriginator.Value = ""
TextBoxNumberOfJobs.Value = ""
ComboBoxProjectTitle.Value = ""
'Hide Window
CreateJobs.Hide
End If
Dim SheetName As String
SheetName = ActiveSheet.Name
Call SheetCleanup
Worksheets(SheetName).Activate
End Sub
Code for SheetCleanup is as follows:
Public Sub SheetCleanup()
'Clan-up on Car Area WorkSheets
Application.ScreenUpdating = False
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is = "Contents Page", "Completed", "VBA_Data", "Front Team Project List", "Mid Team Project List", "Rear Team Project List", "Acronyms"
Case Else
With sh
'set zoom
sh.Activate
ActiveWindow.Zoom = 100
'format columns and rows
.Columns("g:g").NumberFormat = "dd-mm"
.Columns("i:i").NumberFormat = "0"
.Columns("A").ColumnWidth = 27
.Columns("B").ColumnWidth = 50
.Columns("C").ColumnWidth = 50
.Columns("D").ColumnWidth = 21
.Columns("E").ColumnWidth = 27
.Columns("F").ColumnWidth = 21
.Columns("G").ColumnWidth = 10
.Columns("H").ColumnWidth = 15
.Columns("I").ColumnWidth = 22
.Columns("J").ColumnWidth = 17
.Rows("1").RowHeight = 77.2
.Rows("2").RowHeight = 10
.Rows("3").RowHeight = 30
.Rows("4").RowHeight = 10
.Rows("5").RowHeight = 18
.Columns("a:j").HorizontalAlignment = xlCenter
.Columns("b:c").HorizontalAlignment = xlLeft
.Rows("3").HorizontalAlignment = xlCenter
.Rows("5").HorizontalAlignment = xlCenter
.Range("A:J").Validation.Delete
'set data validation for Target Event
Dim ws As Worksheet
Dim wsVBA As Worksheet
Dim range1 As Range, rng As Range
Dim LastRowTargetEvent As Long
Set wsVBA = ThisWorkbook.Worksheets("VBA_Data")
LastRowTargetEvent = wsVBA.Cells(.Rows.Count, "A").End(xlUp).Row
Set range1 = wsVBA.Range("A2:A" & LastRowTargetEvent)
Set ws = ActiveSheet
Set rng = ws.Range("a6:a1000")
With rng.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="='" & wsVBA.Name & "'!" & range1.Address
End With
End With
End Select
Next sh
End Sub
I want to group column name :Check,Code No1,Code 2,Status and total result in Number column according to group using VBA
Data
Result
This is my code:
Sub Sample()
Dim ws As Worksheet, ws1 As Worksheet
Dim LastRowWs As Long, LastRowWs1 As Long, i As Long
Dim Delrange As Range
Application.ScreenUpdating = False
On Error GoTo Whoa
Set ws = Sheets("Sheet1"): Set ws1 = Sheets("Sheet2")
ws1.Cells.Delete
LastRowWs = ws.Range("A" & Rows.Count).End(xlUp).Row
LastRowWs1 = LastRowWs
ws.Range("A1:F" & LastRowWs).Copy ws1.Range("A1")
With ws1
.Columns("A:F").Sort Key1:=.Range("A:F"), Order1:=xlAscending, Key2:=.Range("B2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=4, MatchCase:= _
True, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
:=xlSortNormal
With .Range("A1:F" & LastRowWs1)
.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(5), Replace:=False, _
PageBreaks:=False, SummaryBelowData:=True
.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(5), Replace:=False, _
PageBreaks:=False, SummaryBelowData:=True
.Subtotal GroupBy:=4, Function:=xlSum, TotalList:=Array(5), Replace:=False, _
PageBreaks:=False, SummaryBelowData:=True
End With
LastRowWs1 = .Range("D" & Rows.Count).End(xlUp).Row
'.Rows(LastRowWs1 + 1 & ":" & Rows.Count).ClearContents
.Range("A1:F" & LastRowWs1).Copy
.Range("A1:F" & LastRowWs1).PasteSpecial xlPasteValues
i = LastRowWs1
Do While i > 1
'If InStr(1, .Range("A" & i).Value, "", vbTextCompare) Then
If InStr(1, .Range("D" & i).Value, "Total", vbTextCompare) Then
i = i - 1
Else
If Delrange Is Nothing Then
Set Delrange = .Rows(i - 1)
Else
Set Delrange = Union(Delrange, .Rows(i))
'i = i - 1
End If
End If
i = i - 1
' End If
Loop
If Not Delrange Is Nothing Then Delrange.Delete: Set Delrange = Nothing
LastRowWs1 = .Range("D" & Rows.Count).End(xlDown).Row
'For i = LastRowWs1 To 2 Step -1
For i = 1 To LastRowWs1
If (InStr(1, .Range("D" & i).Value, "Total", vbTextCompare)) Then
.Range("F" & i - 1).Value = .Range("F" & i).Value
If Delrange Is Nothing Then
Set Delrange = .Rows(i)
Else
Set Delrange = Union(Delrange, .Rows(i))
End If
End If
Next i
If Not Delrange Is Nothing Then Delrange.Delete
.Cells.RemoveSubtotal
End With
MsgBox "Vandana, Please check Sheet 'Output' :-)"
LetsContinue:
Application.ScreenUpdating = True
On Error Resume Next
Set ws = Nothing: Set ws1 = Nothing
On Error GoTo 0
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
But result is like this
●★ 100 101 PG is same column group but sum result is separately
See like this:
Somethings you will need to change in Pivot Visualization:
Tabular Form Data
SubTotals Off
Totals Off
Item repeat for each column
Link to File