Posts by shettyrish

    I have a code that has an If-else condition in which only the first few lines of each condition differ, the rest of the operation is the same for both. The following is my common code which I have placed in a sub named 'MsgAnswer' :


    Sub MsgAnswer(SrcWb As Workbook, DestSheet As Worksheet, SrcSheet As Worksheet)


    completed = 0
    Application.StatusBar = "Copying In progress..." & Round(completed, 0) & "% completed"


    'Find the last non-blank cell in row ref
    lnCol = SrcWb.Sheets(SourceName).Cells(ref, Columns.Count).End(xlToLeft).Column


    last = lnCol - 1 'To get penultimate column


    Set DestSheet = DestWb.Sheets(DestName)
    Set SrcSheet = SrcWb.Sheets(SourceName)


    destTotalRows = DestSheet.Cells(Rows.Count, 1).End(xlUp).Row 'Finding last non-blank cell in Column 1 in Destination sheet
    MsgBox "Last row is: " & destTotalRows



    For i = 1 To destTotalRows


    destKey = DestSheet.Cells(i, 1)
    If destKey = "" Then GoTo endTry 'Ignoring blanks while looping through destination sheet


    sourceKey = GetSourceKey(destKey)
    If sourceKey = "" Then GoTo endTry 'Ignoring unmatched values while looping through source sheet


    Debug.Print "DestKey", destKey, "SourceKey", sourceKey


    k = DestSheet.Cells(1, 1).EntireColumn.Find(What:=destKey, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row 'Finding row with Destkey in Destination sheet
    j = SrcSheet.Cells(1, 2).EntireColumn.Find(What:=sourceKey, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row 'Finding row with Srckey in Source sheet


    Debug.Print j, k


    Call CopyRange(SrcSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)), DestSheet.Cells(k, 2), completed) 'Copying the data from Source sheet and pasting it onto destiation sheet
    completed = completed + (100 / steps)
    endTry:
    Next i


    SrcWb.Close


    End Sub
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    This is my uncommon code :
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    1) If answer = vbYes Then
    MyFile = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")


    2) ElseIf answer = vbNo Then


    'change the address to suit
    MyFile = Dir(MyDir & "Estimate*.xls*")
    ChDir MyDir


    Set SrcWb = Workbooks.Open(MyDir + MyFile, UpdateLinks:=0)


    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    The code before the If-else condition :
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


    Sub CopyRange(fromRange As Range, toRange As Range, completed As Double)
    fromRange.Copy
    toRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


    Application.StatusBar = "Copying In progress..." & Round(completed, 0) & "% completed"
    DoEvents
    End Sub


    Sub Automate_Estimate()



    Dim MyFile As String, Str As String, MyDir As String, DestWb As Workbook, SrcWb As Workbook
    Dim Rws As Long, Rng As Range
    Dim DestName As String
    Dim SourceName As String
    Dim completed As Double
    Dim flg As Boolean, sh As Worksheet
    Dim ref As Long
    'Dim DestRowCount As Long
    Dim DestColCount As Long
    Dim lnCol As Long
    Dim last As Long
    Dim destKey As String, sourceKey As String
    Dim destTotalRows As Long
    Dim i As Integer, j, k As Integer
    Dim DestSheet As Worksheet
    Dim SrcSheet As Worksheet



    DestName = "x" 'Name of destination sheet
    SourceName = "y" 'Name of Source sheet
    MyDir = "\Path" 'Default directory path"
    Const steps = 22 'Number of rows copied
    ref = 13 'row in Estimate sheet in which 'Grand Total' is present
    Set DestWb = ThisWorkbook 'Setting Destination workbook




    ' disable certain excel features to speed up the process


    Application.DisplayAlerts = False
    'Application.EnableEvents = False
    ActiveSheet.DisplayPageBreaks = False
    Application.Calculation = xlCalculationManual


    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


    I get an error in my 'MsgAnswer' sub when I call it within If using 'Call MsgAnswer(SrcWb, DestSheet, SrcSheet)' . The error is "ByRef Argument mismatch" and it shows error in the 'for loop' in 'sourceKey = GetSourceKey(destKey)', where GetSourceKey is another function. Any help would be appreciated. Thank you.

    I have attached the image of my mapping table and written these two functions referring to the mapping table that I created : (Table name is "Automation") [ATTACH=JSON]{"alt":"Click image for larger version Name:\tMap.PNG Views:\t2 Size:\t57.0 KB ID:\t1198534","data-align":"none","data-attachmentid":"1198534","data-size":"full","title":"Map.PNG"}[/ATTACH]


    1)
    Function GetRow(rowName As String) As String
    Dim refRange As Range: Set refRange = Sheet14.Range("Automation")
    On Error GoTo errProc
    GetRow = WorksheetFunction.VLookup(rowName, refRange, 2, 0)



    Exit Function



    errProc:
    If Err.Number = 1004 Then
    Err.Raise "5000", "Something bad happened", "Value " & rowName & " not found!!"
    Else
    Err.Raise Err.Number, Err.Source, Err.Description
    End If



    End Function



    2)
    Function GetMap(rowName As String) As String
    Dim refRange As Range: Set refRange = Sheet14.Range("Automation")
    On Error GoTo errProc
    GetMap = WorksheetFunction.VLookup(rowName, refRange, 1, 0)



    Exit Function



    errProc:
    If Err.Number = 1004 Then
    Err.Raise "5000", "Something bad happened", "Value " & rowName & " not found!!"
    Else
    Err.Raise Err.Number, Err.Source, Err.Description
    End If



    End Function


    And this is the snippet of my updated code :


    Dim initial As String



    initial = GetMap(GetRow(wkb.Sheets(SourceName)))


    j = Wb.Sheets(DestName).Cells(1, 1).EntireColumn.Find(What:=initial, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row


    Call CopyRange(Sheets(SourceName).Range("C12:R12"), Wb.Sheets(DestName).Cells(j, 2), completed)
    completed = completed + (100 / steps)


    Call CopyRange(Sheets(SourceName).Range("C22:R22"), Wb.Sheets(DestName).Cells(j, 2), completed)
    completed = completed + (100 / steps)


    Call CopyRange(Sheets(SourceName).Range("C17:R17"), Wb.Sheets(DestName).Cells(j, 2), completed)
    completed = completed + (100 / steps)


    wkb.Close



    When I try the code, I get an error saying "Object doesn't support this property" for the part where I want the function to return a value. I can't figure out how to correct this. Sorry, I am new to VBA. Any help would be appreciated.

    I figured it out


    'Find the last non-blank cell in row 1
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column

    MsgBox "Last Column: " & lCol

    Dim CopyRng As Range
    Set CopyRng = Wb.Sheets(DestName).Range("B1:B57")
    CopyRng.Copy

    For j = 2 To lCol

    Wb.Sheets(DestName).Cells(1, j).PasteSpecial xlPasteFormats

    Next j

    Hi, I have a code that copies data from one workbook to another. Now, the 2nd column in my destination workbook has a certain color format that I need to be applied till the last column with data in it.


    This is my code snippet to find the last non-empty column :


    Dim rLastCell As Range
    Set ws = ThisWorkbook.Sheets(DestName)


    Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)


    Dim LastCol As Long


    LastCol = rLastCell.Column


    MsgBox LastCol


    So, now I want to iterate and paste the format from the 2nd column to all the columns until LastCol.


    Any help would be appreciated. Thank you

    I have created a table mapping the row names in my Destination workbook to row names in my source sheet in the sheet named 'Test'.
    I have attached an image showcasing the mapping in columns A and B respectively.



    [ATTACH=JSON]{"alt":"Map","data-align":"none","data-attachmentid":"1198495","data-size":"full","title":"Map.PNG"}[/ATTACH]


    The destination rows are not successive and there are other rows in between not referring data from the source sheet. The row names in the source sheet are in Column 2 and row names in Destination sheet are in column 1. I need to copy the data from the source sheet rows that have the matching name in the Destination sheet rows as per my mapping from the image attached.


    This is my code:
    Sub Map()


    DestName = "Data Cost Estimate" 'Name of destination sheet


    SourceName = "EST Actuals" 'Name of Source sheet
    MyDir = "Default directory path"


    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .Calculation = xlCalculationManual
    End With


    Set Wb = ThisWorkbook
    Set HeadWS = Wb.Worksheets("Test")
    With HeadWS LastHead = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set HeadRange = .Range("A2:A" & LastHead)
    ReDim Heads(LastHead - 2) '-1 for header row in header map and -1 for 0 based arrary


    Heads = HeadRange.Value


    End With


    answer = MsgBox("If you want to select a specific file click Yes, if you want to go to default path and If you are not sure, click Cancel", vbYesCancel + vbQuestion, "User Specified Path")


    If answer = vbYes Then


    MyFile = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")


    Set wkb = Workbooks.Open(MyFile, UpdateLinks:=0)


    With wkb.Worksheets(SourceName)
    .Rows("1:" & HeadRow - 1).EntireRow.Delete


    For j = .Cells(1, .Columns.Count).End(xlToLeft).Column To 1 Step -1


    If IsError(Application.Match(.Cells(1, j), Heads, False)) Then
    .Columns(j).Delete


    Next


    HeadRange.Offset(, 1).Copy
    .Range("A1").PasteSpecial Paste:=xlPasteAll, Transpose:=True


    End With


    ElseIf answer = vbCancel Then


    Msgbox "Do nothing"


    Exit Sub


    End If


    With Application
    .CutCopyMode = False
    .Calculation = xlCalculationAutomatic


    End With


    ThisWorkbook.Save


    End Sub


    I think it would be easier if I write a function for this process, but I am not sure as to how. The table mapping is in columns A,B respectively. Thank you

    I have written code to copy paste certain rows from one workbook to another. I want a progress bar to show me the progress of the job taking into account each row pasted. For example: If I have to copy-paste 10 rows, then once 1 row is pasted it should show: 10% completed.


    I have used a Second sub which is called in the main sub


    Sub CopyRange(fromRange As Range, toRange As Range, completed As Double)


    fromRange.Copy
    toRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.StatusBar = "Copying In progress..." & Round(completed, 0) & "% completed"
    DoEvents
    End Sub


    This is the Main Sub


    Sub Automate_Estimate()
    Dim completed As Double
    Set Wb = ThisWorkbook
    Const steps = 9 'Number of rows copied
    MyFile = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")
    Set wkb = Workbooks.Open(MyFile, UpdateLinks:=0)


    completed = 0
    Application.StatusBar = "Copying In progress..."
    Call CopyRange(Sheets(SourceName).Range("C12:R12"), Wb.Sheets(DestName).Cells(1, 2), completed)
    completed = completed + (100 / steps)


    Call CopyRange(Sheets(SourceName).Range("C30:R30"), Wb.Sheets(DestName).Cells(24, 2), completed)
    completed = completed + (100 / steps)


    Call CopyRange(Sheets(SourceName).Range("C22:R22"), Wb.Sheets(DestName).Cells(4, 2), completed)
    completed = completed + (100 / steps) .... and so on


    Application.StatusBar = False


    wkb.Close


    DoEvents


    End Sub


    The progress Bar is visible, but it doesn't show the percentage completion. Could somebody help me out with what is wrong with my code? Thank you.