I created a loop that will get the value of one cell to another cell under the same sheet.
The expected result should be this, if the loop runs it will get the 1st value then run my created procedure
next overwrite the same cell get the 2nd value then execute again my created procedure then get 3rd value .. overwrite the cell ..exec proc and so on ...
But my codes only get the last value of the selection.
Public Sub SpecNum()
Dim lrow As Long
Range("A2").Select
lrow = Selection.End(xlDown).Row
For x = 2 To lrow
Range("C2").Value2 = Cells(x, 1).Value2
Next x
Number
End Sub
Public Sub Number()
Dim SpecNum, pref, lastCell As String
Dim lrow As Long
SpecNum = Range("C2").Value2
For x = 2 To 6
Worksheets("Sheet3").Select
pref = Cells(x, "E").Value2
Cells(x, "C").Value2 = SpecNum & pref
Range("C2", Range("C2").End(xlToRight).End(xlDown)).Copy
Next x
Worksheets("Sheet1").Select
Range("A250").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
End Sub
output when moving the number inside the loop.. it doubled/tripled the values
expected result:
I refactored your code a bit. First of all - you should learn how to avoid using Selections (How to avoid using Select in Excel VBA).
Code without Selections is more flexible and less confusing.
Hope it works as you wanted:
Option Explicit
Public Sub SpecNum()
Dim lrow As Long
Dim x As Long
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet3")
lrow = ws.Range("A2").End(xlDown).row
For x = 2 To lrow
ws.Range("C2").Value2 = ws.Cells(x, 1).Value2
Number
Next x
End Sub
Public Sub Number()
Dim SpecNum As String
Dim pref As String
Dim lrow As Long
Dim x As Long
Dim wb As Workbook
Dim ws3 As Worksheet
Dim ws1 As Worksheet
Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("Sheet1")
Set ws3 = wb.Worksheets("Sheet3")
SpecNum = ws3.Range("C2").Value2
For x = 2 To 6
pref = ws3.Cells(x, "E").Value2
ws3.Cells(x, "C").Value2 = SpecNum & pref
Next x
ws3.Range("C2", ws3.Range("C2").End(xlToRight).End(xlDown)).Copy
ws1.Range("A250").End(xlUp).Offset(1, 0).PasteSpecial
End Sub
modified your loop structures. May try
Public Sub SpecNum()
Dim lrow As Long
Range("A2").Select
lrow = Selection.End(xlDown).Row
For X = 2 To lrow
Range("C2").Value2 = Cells(X, 1).Value2
Number
Next X
End Sub
Public Sub Number()
Dim SpecNum, pref, lastCell As String
Dim lrow As Long
SpecNum = Range("C2").Value2
For X = 2 To 6
Worksheets("Sheet3").Select
pref = Cells(X, "E").Value2
Cells(X, "C").Value2 = SpecNum & pref
'Range("C2", Range("C2").End(xlToRight).End(xlDown)).Copy
Range("C" & X, Range("C" & X).End(xlToRight)).Copy
Worksheets("Sheet1").Select
Range("A15").End(xlDown).End(xlDown).End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next X
End Sub
Or may please opt for simplified solution in single procedure
Public Sub SpecNum2()
Dim lrow As Long
Worksheets("Sheet1").Range("A2").Select
lrow = Selection.End(xlDown).Row
TrgRw = 15
For X = 2 To lrow
NumX = Worksheets("Sheet1").Cells(X, 1).Value2
For Y = 2 To 6
TrgRw = TrgRw + 1
Worksheets("Sheet3").Select
pref = Cells(Y, "E").Value2
Cells(Y, "C").Value2 = NumX & pref
'Range("C2", Range("C2").End(xlToRight).End(xlDown)).Copy
Range("C" & Y, Range("C" & Y).End(xlToRight)).Copy
Worksheets("Sheet1").Select
Range("A" & TrgRw).Select
ActiveSheet.Paste
Next Y
Next X
End Sub
Hope It will work
Related
I have a code when runs perfectly by hitting F8 (running steps by steps). However, it doesn't work when hitting F5(running it). I guess it's because my code is kind of in loop but I couldn't figure out what's wrong.
Sub BLKReport()
Dim IRow As Long
Dim lcntr As Long
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim b As Long
Dim r As String
Dim i As Long
Set ws = ThisWorkbook.Worksheets("CSV File")
Set ws1 = ThisWorkbook.Worksheets("Destination")
lrow = Range("A2").End(xlDown).Row
For lcntr = lrow To 1 Step -1
If ws.Cells(lcntr, 9).Value = "25" Then
r = ws.Cells(lcntr, 2).Value
For i = lcntr To 1 Step -1
If ws.Cells(i, 2).Value = r Then
ws.Rows(i).Copy
ws1.Activate
b = i
ws1.Cells(b - 1, 1).Select
ActiveSheet.Paste
ws.Activate
ElseIf ws.Cells(i + 1, 1).Value <> r Then
End If
Next i
End If
Application.CutCopyMode = False
ws.Select
Next
End Sub
I need to loop through a column and if a conditions if met copy cell from one sheet to another.
I'm finding problems with the incremental..
In this case double the results.
Thank you in advance.
KR
Sub copycell()
Dim iLastRow As Long
Dim i As Long
Dim erow As Long
erow = 1
iLastRow = Worksheets("Clientes").Cells(Rows.Count, "C").End(xlUp).Row
For i = 13 To iLastRow
If Sheets("Clientes").Cells(i, 3) = "0" Then
Worksheets("Ficheros").Range("B" & erow).End(xlUp).Offset(1) = Sheets("Clientes").Cells(i, 4)
erow = erow + 1
End If
Next i
End Sub
Why not use Autofilter to filter the column C and if the autofilter returns any rows, copy them to the destination sheet?
See if something like this works for you...
Sub CopyCells()
Dim wsData As Worksheet, WsDest As Worksheet
Dim iLastRow As Long
Application.ScreenUpdating = False
Set wsData = Worksheets("Clientes")
Set WsDest = Worksheets("Ficheros")
iLastRow = wsData.Cells(Rows.Count, "C").End(xlUp).Row
wsData.AutoFilterMode = False
With wsData.Rows(12)
.AutoFilter field:=3, Criteria1:="0"
If wsData.Range("D12:D" & iLastRow).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
wsData.Range("D13:D" & iLastRow).SpecialCells(xlCellTypeVisible).Copy
WsDest.Range("B" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
End If
End With
wsData.AutoFilterMode = False
Application.CutCopyMode = 0
Application.ScreenUpdating = True
End Sub
You can achieve your result with AutoFilter, but my answer is trying to resolve your code using the For loop.
Modified Code
Option Explicit
Sub copycell()
Dim iLastRow As Long
Dim i As Long
Dim erow As Long
' get first empty row in column B in "Ficheros" sheet
erow = Worksheets("Ficheros").Range("B" & Rows.Count).End(xlUp).Row + 1
With Worksheets("Clientes")
iLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For i = 13 To iLastRow
If .Cells(i, 3) = "0" Then
Worksheets("Ficheros").Range("B" & erow) = .Cells(i, 4)
erow = erow + 1
End If
Next i
End With
End Sub
I may have up to 8 unique values in column D. I am looking for a code that will copy & paste each row with unique value to a new sheet.
So I may have up to 8 new sheets.
Could you help me to build the code that will do that?
This is what I have so far:
Option Explicit
Sub AddInstructorSheets()
Dim LastRow As Long, r As Long, iName As String
Dim wb As Workbook, ws As Worksheet, ts As Worksheet, nws As Worksheet
Dim i As Integer
Dim m As Integer
'set objects
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Set ts = Sheets("Master")
'set last row of instructor names
LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'add instructor sheets
On Error GoTo err
Application.ScreenUpdating = False
For r = 17 To LastRow 'assumes there is a header
iName = ws.Cells(r, 4).Value
With wb 'add new sheet
ts.Copy After:=.Sheets(.Sheets.Count) 'add template
Set nws = .Sheets(.Sheets.Count)
nws.Name = iName
Worksheets(iName).Rows("17:22").Delete
Worksheets("Master").Activate
Range(Cells(r, 2), Cells(r, 16)).Select
Selection.Copy
m = Worksheets(iName).Range("A15").End(xlDown).Row
Worksheets(iName).Cells(m + 1, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Next r
err:
ws.Activate
Application.ScreenUpdating = True
End Sub
The thing is that this macro is creating new sheets, which is not necessary. I only want to make following.
If you find a unique value in column D (which will have exact name as other sheet), find this sheet and paste whole row in there.
Sub CopyFromColumnD()
Dim key As Variant
Dim obj As Object
Dim i As Integer, lng As Long, j As Long
Dim sht As Worksheet, mainsht As Worksheet
Set obj = CreateObject("System.Collections.ArrayList")
Set mainsht = ActiveSheet
With mainsht
lng = .Range("D" & .Rows.Count).End(xlUp).Row
With .Range("D1", .Range("D" & lng))
For Each key In .Value
If Not obj.Contains(key) Then obj.Add key
Next
End With
End With
For i = 0 To obj.Count - 1
Set sht = Sheets.Add(After:=Sheets(Sheets.Count))
sht.Name = obj(i)
For j = 1 To lng
If mainsht.Cells(j, 4).Value = obj(i) Then
mainsht.Rows(j).EntireRow.Copy Destination:=Range("A1")
Exit For
End If
Next
Next
End Sub
Ok, I did the workaround. I have created a list of unique values in a separate sheet.
Sub copypaste()
Dim i As Integer
Dim j As Integer
LastRow = Worksheets("Master").Range("D17").End(xlDown).Row
For i = 17 To LastRow
For j = 2 To 10
Workstream = Worksheets("Database").Cells(j, 5).Value
Worksheets("Master").Activate
If Cells(i, 4) = Worksheets("Database").Cells(j, 5).Value Then
Range(Cells(i, 2), Cells(i, 16)).Select
Selection.Copy
Worksheets(Workstream).Cells(1, 1).PasteSpecial Paste:=xlPasteValues
Else
End If
Next j
Next i
End Sub
Thank you everyone for help and your time!
I have created a useform which helps me to change the column width and row height of active sheet or all the sheets (based on the selection option in Frame3) from column B or Column C (based on the selection option in Frame2) based on the text entered in the textbox1 and textbox2.
I have attacahed userform for your reference.
This code is working fine, however if I only need to change the column width not the row height then when I key in the column width and leave the row height empty then its giving me error on the below mentioned line:
Selection.Cells.RowHeight = Me.TextBox2.Value
Please let me know how should I modify my code that even if any of the textbox value is left blank (i.e if I only want to change the column width and maintain the row height as-is) then it should not give me error.
Userform Code:
Dim exworkb As Workbook
Dim xlwksht As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim firstrowDB As Long
Dim Z As Integer
Dim ShtNames() As String
Private Sub CommandButton1_Click()
If Me.OptionButton3.Value = True Then
If Me.OptionButton1.Value = True Then
Call rowcolactivesheetb
Selection.Cells.RowHeight = Me.TextBox2.Value
Selection.Cells.ColumnWidth = Me.TextBox1.Value
ElseIf Me.OptionButton2.Value = True Then
ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count)
For Z = 1 To Sheets.Count
ShtNames(Z) = Sheets(Z).Name
If ShtNames(Z) <> "Trans_Letter" And ShtNames(Z) <> "Cover" And ShtNames(Z) <> "Abbreviations" And InStr(ShtNames(Z), "_Index") = 0 Then
Sheets(Z).Select
lastrow1 = Sheets(Z).Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = Sheets(Z).Cells(1, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.Sheets(Z).Range(Sheets(Z).Cells(1, 2), Sheets(Z).Cells(lastrow1, lastcolumn1)).Select
Selection.Cells.RowHeight = Me.TextBox2.Value
Selection.Cells.ColumnWidth = Me.TextBox1.Value
End If
Next Z
End If
End If
If Me.OptionButton4.Value = True Then
If Me.OptionButton1.Value = True Then
Call rowcolactivesheetc
Selection.Cells.RowHeight = Me.TextBox2.Value
Selection.Cells.ColumnWidth = Me.TextBox1.Value
ElseIf Me.OptionButton2.Value = True Then
ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count)
For Z = 1 To Sheets.Count
ShtNames(Z) = Sheets(Z).Name
Sheets(Z).Select
If ShtNames(Z) <> "Trans_Letter" And ShtNames(Z) <> "Cover" And ShtNames(Z) <> "Abbreviations" And InStr(ShtNames(Z), "_Index") = 0 Then
lastrow1 = Sheets(Z).Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = Sheets(Z).Cells(1, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.Sheets(Z).Range(Sheets(Z).Cells(1, 3), Sheets(Z).Cells(lastrow1, lastcolumn1)).Select
Selection.Cells.RowHeight = Me.TextBox2.Value
Selection.Cells.ColumnWidth = Me.TextBox1.Value
End If
Next Z
End If
End If
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module code:
Sub rowcolactivesheetb()
Dim exworkb As Workbook
Dim xlwksht As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim firstrowDB As Long
With ActiveSheet
lastrow1 = .Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(1, 2), .Cells(lastrow1, lastcolumn1)).Select
End With
End Sub
Sub rowcolactivesheetc()
Dim exworkb As Workbook
Dim xlwksht As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim firstrowDB As Long
With ActiveSheet
lastrow1 = .Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(1, 3), .Cells(lastrow1, lastcolumn1)).Select
End With
End Sub
I have the following Macro:
Sub Remove_Junk_Data()
Call Open_Workbook
Call Scrub_Master
Call Scrub_Change_History
Call Scrub_Update
Call Scrub_ExistingOwnership
Call Save_Scrubbed
End Sub
Sub Open_Workbook()
Workbooks.Open "https://company.sharepoint.com/sites/project/subproject/subsubproject/subsubprojecttool/tooloutput/tooloutput.xlsx"
Workbooks("tooloutput.xlsx").Activate
End Sub
Sub Scrub_Master()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("Master").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Scrub_Change_History()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("Change History").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Scrub_Update()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("Update").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Scrub_ExistingOwnership()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("ExistingOwnership").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Save_Scrubbed()
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
"https://company.sharepoint.com/sites/project/subproject/subsubproject/subsubprojecttool/tooloutput/tooloutput.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = False
Workbooks("Master_FM_Update.xlsx").Close
End Sub
If I comment out the "Open_Workbook" sub and manually open the workbook then run the remainder of Remove_Junk_Data(), it works perfectly.
If I try to run Remove_Junk_Data with Open_Workbook active, then no errors are thrown, but the 4 middle subs dont do anything...
Has anyone ran into anything like this? Did you find a resolution? I want to click a button and have all 6 subs do their thing correctly...
Edit: With input, new macro, and it works! Thanks guys!:
Sub Remove_Junk_Data()
Workbooks.Open "https://company.sharepoint.com/sites/project/subproject/subsubproject/Subsubprojecttool/tooloutput/tooloutput.xlsx"
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
With Workbooks("tooloutput.xlsx").Sheets("Master")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
With Workbooks("tooloutput.xlsx").Sheets("Change History")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
With Workbooks("tooloutput.xlsx").Sheets("Update")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
With Workbooks("tooloutput.xlsx").Sheets("ExistingOwnership")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
End Sub
I have done some changes to your code, the comments in the procedure intent to explain the changes. My advice is that besides what you are getting from sites like Stackoverflow you should also read the corresponding documentation to achieve a deeper understanding of the concepts and resources used. Nevertheless, do not hesitate to ask question as you go forward in developing your programing skills.
The code below consolidates all what you are trying to do in one procedure, there you'll see how to run repetitive code for a series of values (i.e. worksheets in this case)
Suggest to visit the following pages:
Variables & Constants, Application Object (Excel), Excel Objects
With Statement, For...Next Statement, For Each...Next Statement,
If...Then...Else Statement
Worksheets Object (Excel), Worksheet Object (Excel), Range Object (Excel)
Sub Remove_Junk_Data()
Rem Use an Array Variable to List all the worksheets you want to work with
Dim aWsh As Variant, vItm As Variant
aWsh = Array("Master", "Change History", "Update", "ExistingOwnership")
Rem Declare Object Variables
Dim Wbk As Workbook
Dim Wsh As Worksheet
Dim lRowLst As Long
Dim lRow As Long
Rem Open Workbook & Set Workbook Object Variable
Set Wbk = Workbooks.Open("https://company.sharepoint.com/sites/project/subproject/subsubproject/subsubprojecttool/tooloutput/tooloutput.xlsx")
Rem Loop throught the worksheet list and process each one
For Each vItm In aWsh
Rem Set Worksheet Object Variable
Set Wsh = Wbk.Worksheets(vItm)
With Wsh
lRowLst = Cells(Rows.Count, "A").End(xlUp).Row
For lRow = lRowLst To 1 Step -1
With .Cells(lRow, 1)
If .Value2 = Empty And Not (.HasFormula) Then .EntireRow.Delete
End With: Next: End With: Next
Application.DisplayAlerts = False
Wbk.Save
Application.DisplayAlerts = True
Workbooks("Master_FM_Update.xlsx").Close
End Sub