Вы находитесь на странице: 1из 8

Private Sub CommandButton2_Click() Dim FromFile As Workbook Dim ToFile As Workbook Dim status As String Set FromFile = ActiveWorkbook

lastRow = LastRowInOneColumn(1) If lastRow < 1 Then col = lastRow + 1 Else col = lastRow End If Set ToFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\To File.xlsx", UpdateLinks:=False, ReadOnly:=False) Worksheets("Sheet1").Select For n = 1 To lastRow FromFile.Activate If Cells(n, 1) <= 2 Then status = "Old" ToFile.Activate ActiveSheet.Cells(n, 2) = status End If ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(col, 1).Select Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy ToFile.Save col = col + 1 Next n ToFile.Close False End Sub -----------------------------------------------------------Private Sub CommandButton1_Click() lastColumn = LastColumnInOneRow(1) If lastColumn > 0 Then MsgBox ("Hello world" & lastColumn) End If End Sub Private Sub CommandButton2_Click() Dim FromFile As Workbook Dim ToFile As Workbook Dim status As String Set FromFile = ActiveWorkbook lastRow = LastRowInOneColumn(1) If lastRow < 1 Then col = lastRow + 1 Else col = lastRow End If Set ToFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\To File.xlsx", UpdateLinks:=False, ReadOnly:=False) Worksheets("Sheet1").Select For n = 1 To lastRow If Not Cells(n, 2) = "" Then

If Cells(n, 2) = "D" Then status = "Deleted" ToFile.Activate ActiveSheet.Cells(n, 2) = status FromFile.Activate ActiveSheet.Cells(n, 2) = status ElseIf Cells(n, 2) = "U" Then status = "Updated" FromFile.Activate ActiveSheet.Cells(n, 2) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select 'ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 2) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy End If ElseIf Cells(n, 2) = "" Then status = "Done" FromFile.Activate ActiveSheet.Cells(n, 2) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select 'ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 2) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy End If col = col + 1 Next n ToFile.Save ToFile.Close False End Sub Public Function LastRowInOneColumn(col) Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).row End With LastRowInOneColumn = lastRow End Function Public Function LastColumnInOneRow(row) Dim lastColumn As Long With ActiveSheet lastColumn = .Cells(row, .Columns.Count).End(xlToRight).Column 'lastColumn = Cells.Find(What:="", After:=[A1], SearchOrder:=xlByColumns, Se archDirection:=xlNext).Column End With LastColumnInOneRow = lastColumn End Function

--------------------------------------------------------------------------------------------Private Sub CommandButton1_Click() lastColumn = LastColumnInOneRow(1) If lastColumn > 0 Then MsgBox ("Hello world" & lastColumn) End If End Sub Private Sub CommandButton2_Click() Dim FromFile As Workbook Dim ToFile As Workbook Dim status As String Set FromFile = ActiveWorkbook lastRow = LastRowInOneColumn(1) If lastRow < 1 Then col = lastRow + 1 Else col = lastRow End If Set ToFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\To File.xlsx", UpdateLinks:=False, ReadOnly:=False) Worksheets("Sheet1").Select For n = 1 To lastRow If Not Cells(n, 1) = "" Then If Cells(n, 1) = "D" Then status = "Deleted" ToFile.Activate ActiveSheet.Cells(n, 1) = status FromFile.Activate ActiveSheet.Cells(n, 1) = status ElseIf Cells(n, 1) = "U" Then status = "Updated" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select 'ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy ElseIf Cells(n, 1) = "New" Then status = "Done" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select 'ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy

End If End If 'col = col + 1 Next n ToFile.Save ToFile.Close False End Sub Public Function LastRowInOneColumn(col) Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).row End With LastRowInOneColumn = lastRow End Function Public Function LastColumnInOneRow(row) Dim lastColumn As Long With ActiveSheet lastColumn = .Cells(row, .Columns.Count).End(xlToRight).Column 'lastColumn = Cells.Find(What:="", After:=[A1], SearchOrder:=xlByColumns, Se archDirection:=xlNext).Column End With LastColumnInOneRow = lastColumn End Function -----------------------------------------------------------------------------------------------------Private Sub CommandButton2_Click() Dim FromFile As Workbook Dim ToFile As Workbook Dim status As String Set FromFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\ Sindhu.xlsx", UpdateLinks:=False, ReadOnly:=False) Set ToFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\Da tabase.xlsx", UpdateLinks:=False, ReadOnly:=False) FromFile.Activate lastRow = LastRowInOneColumn(1) 'If lastRow < 1 Then ' col = lastRow + 1 'Else ' col = lastRow 'End If ToFile.Activate Worksheets("Sheet1").Select For n = 1 To lastRow FromFile.Activate If Not Cells(n, 1) = "" Then If Cells(n, 1) = "D" Then status = "Deleted" ToFile.Activate ActiveSheet.Cells(n, 1) = status FromFile.Activate ActiveSheet.Cells(n, 1) = status ElseIf Cells(n, 1) = "U" Then status = "Updated" FromFile.Activate

ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select 'ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy ElseIf Cells(n, 1) = "New" Then status = "Done" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select 'ActiveSheet.Cells(n, 1).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy End If End If 'col = col + 1 Next n ToFile.Save ToFile.Close False FromFile.Save FromFile.Close False End Sub Public Function LastRowInOneColumn(col) Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).Row End With LastRowInOneColumn = lastRow End Function -----------------------------------------------------------------------------------------Public Sub Main() Call CommandButton2_Click("C:\Users\divya.jakkaraju\Desktop\Sindhu.xlsx", "Sindh u") Call CommandButton2_Click("C:\Users\divya.jakkaraju\Desktop\Swetha.xlsx", "Sweth a") End Sub Public Sub CommandButton2_Click(path, sheet) Dim FromFile As Workbook Dim ToFile As Workbook Dim status As String Set FromFile = Application.Workbooks.Open(path) ', UpdateLinks:=False, ReadO nly:=False) FromFile.Activate lastRow = LastRowInOneColumn(1)

Set ToFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\Da tabase.xlsx") ', UpdateLinks:=False, ReadOnly:=False) ToFile.Activate Worksheets(sheet).Select For n = 1 To lastRow FromFile.Activate If Not Cells(n, 1) = "" Then If Cells(n, 1) = "D" Then status = "Deleted" ToFile.Activate ActiveSheet.Cells(n, 1) = status FromFile.Activate ActiveSheet.Cells(n, 1) = status ElseIf Cells(n, 1) = "U" Then status = "Updated" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy ElseIf Cells(n, 1) = "New" Then status = "Done" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy End If End If Next n ToFile.Save ToFile.Close False FromFile.Save FromFile.Close False End Sub Public Function LastRowInOneColumn(col) Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).Row End With LastRowInOneColumn = lastRow End Function -------------------------------------------------------------------------------------------

Public Sub Main() Call CommandButton2_Click("C:\Users\divya.jakkaraju\Desktop\Sindhu.xlsx", "Sindh u") Call CommandButton2_Click("C:\Users\divya.jakkaraju\Desktop\Swetha.xlsx", "Sweth a") End Sub Public Sub CommandButton2_Click(path, sheet) Dim FromFile As Workbook Dim ToFile As Workbook Dim status As String Set FromFile = Application.Workbooks.Open(path) ', UpdateLinks:=False, ReadO nly:=False) FromFile.Activate lastRow = LastRowInOneColumn(1) Set ToFile = Application.Workbooks.Open("C:\Users\divya.jakkaraju\Desktop\Da tabase.xlsx") ', UpdateLinks:=False, ReadOnly:=False) ToFile.Activate Worksheets(sheet).Select For n = 1 To lastRow FromFile.Activate If Not Cells(n, 1) = "" Then If Cells(n, 1) = "D" Then status = "Deleted" ToFile.Activate ActiveSheet.Cells(n, 1) = status FromFile.Activate ActiveSheet.Cells(n, 1) = status ElseIf Cells(n, 1) = "U" Then status = "Updated" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy ElseIf Cells(n, 1) = "New" Then status = "Done" FromFile.Activate ActiveSheet.Cells(n, 1) = status ActiveSheet.Range(Cells(n, 1), Cells(n, 3)).Select Selection.Copy ToFile.Activate ActiveSheet.Cells(n, 1).Select ActiveSheet.Cells(n, 1) = status Selection.PasteSpecial xlPasteValues Application.CutCopyMode = xlCopy End If End If Next n ToFile.Save ToFile.Close False

FromFile.Save FromFile.Close False End Sub Public Function LastRowInOneColumn(col) Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).Row End With LastRowInOneColumn = lastRow End Function

Вам также может понравиться