I lang tid har samarbejdsbranchen fokuseret på at forbedre enhedsfunktioner – bedre kameraer, klarere lyd og smartere software. Men den virkelige forvandling handler ikke om funktioner.
Option Explicit ' Purpose: Loop row, column on sheet 2 and copy data to sheet 1 ' Parameter: None Sub CopySheet2toSheet1() Dim sourceSheet As Worksheet ' Sheet to copy data from Dim destinationSheet As Worksheet ' Sheet to insert data to Dim maxColumn As Long ' Number of columns in sheet Dim maxRow As Long ' Number of rows in sheet Dim rowCount As Long ' Var to loop trough rows Dim colCount As Long ' Var to loop trough columns
' Initialization Set sourceSheet = ThisWorkbook.Worksheets(2) ' If reference by name: worksheets("Sheet2") Set destinationSheet = ThisWorkbook.Worksheets(1) ' If reference by name: worksheets("Sheet1") Call applicationInit ' Turns off screenupdating and calculation maxRow = LastRow(sourceSheet) maxColumn = LastColumn(sourceSheet) destinationSheet.Cells.Clear ' Clears destination sheet
' Logic For rowCount = 1 To maxRow Step 1 For colCount = 1 To maxColumn Step 1 destinationSheet.Cells(rowCount, colCount) = "Copied: " & sourceSheet.Cells(rowCount, colCount) Next Next
' Termination MsgBox (sourceSheet.Name & " copied to " & _ destinationSheet.Name & ": " & _ "(rows, columns) = (" & maxRow & ", " & maxColumn & ")") Call applicationEnd ' Turns on screenupdating and calculation End Sub ' Purpose: Turns of stuff to increase performance ' Parameter: None Sub applicationInit() Application.Calculation = xlCalculationManual Application.ScreenUpdating = False ' Update off -> speeds up processing End Sub ' Purpose: Turns on stuff when work completed ' Parameter: None Sub applicationEnd() Application.ScreenUpdating = True ' Update on Application.Calculation = xlCalculationAutomatic ' Calculation on End Sub ' Purpose: Return number of rows on worksheet ' Parameter: curSH - Sheet to work on Public Function LastRow(curSH As Worksheet) As Long LastRow = curSH.UsedRange.Rows.Count End Function ' Purpose: Return number of columns on worksheet ' Parameter: curSH - Sheet to work on Public Function LastColumn(curSH As Worksheet) As Long LastColumn = curSH.UsedRange.Columns.Count End Function
Jeg er lidt i tvivl om du vil tilgå cellevis, eller rækkevis - men mit første forslag gik på celle efter celle!
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.