Using Loops On Current Code
Jul 27, 2006
I currently have the following macro - as you can see it is quite repetitive. I know it is possible to do this using a loop any ideas on how to do this would be appreciated.
Sub Clean()
Rows("4:4").Select
Selection.Delete Shift:=xlUp
Rows("5:5").Select
Selection.Delete Shift:=xlUp
Rows("6:6").Select
Selection.Delete Shift:=xlUp
Rows("7:7").Select
Selection.Delete Shift:=xlUp
I thought that this would work - but not so probably some basic syntax error:
Sub Runny()
For x = 4 To 17
Rows("x:x").Select
Selection.Delete Shift:=xlUp
Next x
End Sub
View 9 Replies
ADVERTISEMENT
Apr 6, 2014
Not come across this issue before where the Code works once then fails to iterate!
In attached file 140405 Loop not working.xlsm, for each cell in C4:C27 on the Inventory Sheet, the Code should:
Look in the Data Input sheet.
Find the Item Number in Col B corresponding to the number in Col A of the Inventory Sheet.
Find the quantity on the Data Input sheet corresponding to that number.
Copy the quantity into Col H of the Inventory sheet.
The Code works for the first row then throws error message "Run Time error 13" Type Mismatch!
VB:
Sub OFFLOAD()
Dim Dest As Range, Dest1 As Range, oCell As Range
Set Dest = Sheet2.Range("C:C")
Set Dest1 = Sheet2.Range("B:B")
Sheets("INVENTORY").Select
[Code] .....
View 2 Replies
View Related
May 23, 2014
I am having some trouble with some code I am trying to write for some calculations. I am trying to make excel iterate through a range of cells in 2 columns and compare the cells in each row of the column to a set different number. If both the numbers in the row pass the test then I add them to a count. I know it is simple but I am really struggling with my basic vba knowledge to correctly iterate through 2 columns.
A
B
5
8
8
4
2
9
9
9
For example say my "cutoffs" are a value greater than 2 for numbers in column A and a value greater than 5 for numbers in column B. The program then should return me a value of 2 for this example.
Option Explicit
Function cutoff(phi_cutoff As Double, sw_cutoff As Long, gross As Double)
Dim count As Double
Dim my_range1 As Range
Dim my_range2 As Range
Dim c1 As Range
Dim c2 As Range
Dim value1 As Double
[Code] ............
View 3 Replies
View Related
Feb 15, 2007
I have a userform which displays data in a sequence for my workbook.
For now it is performing almost the way i wish, the data is displayed as the code loops thruogh a range (K6:N2000) when the condition is met.
Private Sub CommandButton1_Click()
For r = 6 To 2000
For c = 11 To 14
Application.ScreenUpdating = False
If Cells(r, c).Offset(1, 0) Cells(r, c) Then
Cells(r, c).Select
Dim sWhat As String
Dim wWhat As String
sWhat = ActiveCell.End(xlUp).Value
MsgBox "Generate " & sWhat & " " & ActiveCell.Value & "'s" & " requirement data.", _
vbMsgBoxSetForeground, ("Azone requirment")..................
View 9 Replies
View Related
Aug 3, 2014
I have the following code linked to a drop down list in my workbook.
[Code].....
It populates textboxes on the sheet with a lookup value result, the lookup value is taken from the dropdown box. The problem I have is any other excel workbooks that are open at the same time, the code is trying to run against those workbooks when I edit them, I can't seem to restrict the code to run only against the workbook in which the dropdown box which the code is linked to, exists in. I have tried moving the code to a module in the workbook but then the code does not seem to work as it does know which workbook the combo box and text boxes which should be filled with the lookuip result belong in
Link to workbook is [URL] .....
View 5 Replies
View Related
Dec 1, 2011
Code:
Sub Final()
Dim NameCell As Range
For Each NameCell In Sheets("! Names").Range("B1", Sheets("! Names").Cells(Rows.Count, "B").End(xlUp))
[Code]....
I want to pull data from ! Names. This code already Renames a new sheet from the ! Names sheet and also puts there name in cell C2 and phone number into cell C3 of the created sheet. I want to put more data from the ! Names sheet. How would i go about adjusting current code to do that?
View 4 Replies
View Related
Jan 22, 2013
What is the code to identify the current worksheet name? I want to store it in a variable so I can return to the worksheet later. I thought that the code was this:
myWorkSheetName = worksheet.name
:
:
Sheets(myWorkSheetName).Select
I receive a "Run-time error 424: Object required" message on the worksheet.name statement.
View 2 Replies
View Related
Apr 17, 2009
I know this is a simple question for all VBA droids...I guess the challenge is in how quickly I'll receive an answer. I hear the turn-around time on this board is pretty impressive. So here goes...
What is the VBA code for recording the current date without the date recalculating when the form is re-opened. I know the formula for automatic entry [=today()], but I need the date to stay the same. I can assign the date to any cell, so you may theorize any cell in the code. Remember...the trick is getting the date to stay the same once it's been automatically entered.
View 9 Replies
View Related
Oct 15, 2009
I need a macro that keeps only the top 25 rows in the current range and deletes the rest.
View 9 Replies
View Related
Feb 15, 2010
I want to schedule a print, for lets say every Sunday at midnight. I have managed to find some what of a solution by using the following vbscript and scheduling a print to default printer. Works perfectly if that is all I wanted.
Dim objExcel, objWorkbook, strFile
' Connect to Excel
Set objExcel = CreateObject("Excel.Application")
strFile = "C: est.xls"
' Open the Excel File for Printing
Set objWorkbook = objExcel.Workbooks.Open _
(strFile)
' Print to Default Printer
Set objSheet = objExcel. ActiveWorkbook.Worksheets(1)
objSheet.PrintOut
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
I do not want to print to an actual printer, therefore if I set my default printer as the document writer, the vbscript gets it as far as the "Save As" page. Is there a way I can add to this vbscript so it will save as well? Can I get it to save as "name"&[DATE], thereby having it save as a different name each time?
Can I change this vbscript to print to a named printer rather then a default printer? I am sure a PDF convertor may be of some assistance with this, but I do not have an PDF converter on my computer, and as this will be carried out on a computer that I will not have administration rights, I can not download one.
View 2 Replies
View Related
Dec 27, 2012
I've been heavily updating my Budget file I've written in excel. The code I'm working with and having slight trouble with is as follows
Try this formula. Column letter & Row number are for example only. Array formulas will have curly brackets {} emcompassing the ENTIRE formula. You do not add these, you Enter the formula with Ctrl+Shift+Enter and Excel will add them (details: Array formulas). Many more Excel Formulas
Z100 =IF(TODAY()>DATE(2013,1,31),"January's Ending Balance is:",IF(TODAY()<=DATE(2013,1,31),"January's Current Balance is:"))
Basically, if the current date is after the last day of a given month (in this case January 31), then the cell should display "January's Ending Balance is:", if between January 1, (current year), and January 31, (current year), it should display "January's Current Balance is:". The code above does work great, but I need it to look at the current year according to the computer's date, and go by that, instead of having to change the code where it says 2013 to 2014 etc every year. This would be a hassle, as I have a tab coded for each month of the year. Id rather it be automated.
View 8 Replies
View Related
Jul 31, 2007
I want code to:
Open EstimatingSheet.xls
SaveAs current workbook as Range B13 &".xls"
Open EstimatingSheet.xls workbook from somewhere on C:
Then close the one with B13.xls
View 9 Replies
View Related
Sep 8, 2008
I have a piece of code that prompts me to choose which column to be filled bottom up, I would like to modify this and add it in as a macro so that it fills columns H and columns I in a bottom-up fashion
View 13 Replies
View Related
Jul 21, 2009
i have a sheet for monitoring sickness. i have used a macro before which automatically selects a range and emails it to recipients when i press a button. what i would like to do is have excel automatically choose the range for me based on today's day. in the attached example i would like column "A" copied along with the 5 columns before today's date, today's date column and also the 5 columns after today's date. paste the lot into an email and send. (i have highlghted the parts i would like copied and pasted into an email based on today's date (21st july).
View 4 Replies
View Related
Sep 19, 2007
Is it possible to display both the current code line number and the total line count of a macro while it's running?
I was thinking it would be handy for a progress indicator if the ProgressBar max value could be set to the LineCount total of the procedure and each line of code would increment the progressbar as the macro ran.
View 9 Replies
View Related
Mar 30, 2013
I am currently trying to adapt the following code which is in VBA (Sheet1). It currently ensures mandatory cell entry of cell C2 before saving, and if this is not done then it would not save the item into the relevant directory. I would like to adapt this code so that cells B2 to R2 are all mandatory, and if they are not filled in a message box with the cell title (which would be in B1 to R1) would pop up. If the cells are not filled in then it will not save (the same function it carries out for cell c2 currently). The code is attached to a command button, i have tried to make the additional cells mandatory by playing around with the If Trim(.cells(2, 3).value) part of the code with the AND function etc however i have had not had any luck.
View 7 Replies
View Related
Apr 11, 2014
The code as it stands allows me to push a button and it immediately picks up on a meeting category and date of meeting, then sends emails to those that are supposed to attend based on a date that is within 7 days from the current date of pressing that button. HOWEVER; it seems to still send the email when the date has passed...is there a line I can put in this code to make it so that every date that has already passed will be discounted from future emails?
The code is as follows:
[Code].....
View 1 Replies
View Related
Mar 11, 2014
I needed a code that would input the current date and time in the cells in column N whenever changes were made to any cells in the row from columns A to M. For example, if I change a name in cell 6D, then cell 6N would automatically change to the current date and time.
I found a useful code on a forum (maybe here, don't know for certain) and modified it to suit my needs (see below). I am however now getting a debugging error suggesting that the second line that reads "Private Sub Worksheet_Calculate()" is causing an error.
View 11 Replies
View Related
Feb 4, 2014
I’m working on a project using Microsoft Excel 2010 and I want to add some features to facilitate saving and retrieving files process.
1.How can I save the daily created workbooks (Assume 15 files a day) in order to contain the current date (and time if possible) linked with certain cell(s) I have at my workbook forming the file name? (XYZ 2-4-2014) and/or (ABC 2-4-2014 23:11) and so on …
2.I’ve been through some other posts and I found VB code which saves the active file into specific path, but it is only useful for single workbook because multiple files are getting overwritten automatically. Is there a code which allows multiple/different files saving & creates daily folders?
View 4 Replies
View Related
Jan 6, 2013
I have series of data values like below. I have to find Maximum, Minimum values for each of these values.
9430
9822
10070
[Code].....
View 2 Replies
View Related
Apr 13, 2007
how can i make a loop throught the G colum and to check if the cell before the currect is bigger/ lower or equal to the currect cell. and if its higher then an arrow will be shown '^' if its lower an upside down arrow will be show 'v'
equal will show -> arrow. example:
G2 = 200
G3 = ^ 300
G4 = -> 300
G5 = v 209
so how can i change the G3 to G4 G5 etc.. in a loop in the: Range("G3").Select command? and i need to change the
.Value = "=$G$2"
to
Value = "=$G$3"
Value = "=$G$4"
etc...
in 2 places................
View 4 Replies
View Related
Oct 18, 2008
i have this code that upon opening of workboot it generates the next number thats available on sheet 2 i then enter the data that i need to and click the button it then transfers to sheet2 and prints 2 copies and then clears the cells that i have input data to
what i really need it to do is exactly as it does right now but to then change the next number available to what it is ....
View 14 Replies
View Related
Aug 11, 2009
I seem to be having trouble creating a nested loop. It seemed simple enough in my brain holder, however, in real life...Well I am here aren't I. Here is my
View 2 Replies
View Related
Aug 20, 2009
First of all, I'm completely new to both this forum and VBA. I have just done a programming course in java before. I hope you will forgive me if this have already been posted. No to my problem.
I am tryring to use different projected values to project other subparts of those units, which is done in the sheet mean needed weekly. Tje values are taken from projections. The answer that I get from mean needed weekly should be copied to a third sheet.
This is the code that I am using right know, but I cant get the for loop to work (or more exactly to be able to place the "i" in my text without making compilation errors).
View 9 Replies
View Related
Oct 9, 2009
One of the multitude of areas where I still need drastic improvement is with efficiency. Specifically, avoiding loops.
Are any of you aware of any good, basic level sites, blogs, posts, etc., about better methods or processes? I'm not particularly experienced with programming (I've been a software TESTER for 13 years and just started some programming in recent months), so the more basic the better.
View 2 Replies
View Related
Oct 2, 2008
I have recorded a macro that cleans up my data that I receive from an external source and inserts two columns.
I recorded a seperate macro to execute an "if formula" that evaluates Column A and B and if A is blank return the value in B, else return the value in A. I wrote the formula =if(a1=" ",b1,a1).
I referred to the text I got in the class and found that I can use a "Next Loop". I added to the formating macro the Next Loop critera and instead of inserting text I inserted the above formula.
It ran and returned data, however it was not in the column I intended, it cascaded off to the side of my data in a step down (kinda like stairs).
View 9 Replies
View Related
Nov 3, 2009
I need to maintain a KPI in excel2000 which details weekly progress, I normally do this using some condition formatting and hiding cells, I have to be honest its not pretty.
What I would like to do is to use the colorindex function to show the wk breakdown status and a Do loop on my target range, I think!...... I've a basic example below,
If I could show the benefits to my boss he might even let me take a VBA course.....
View 9 Replies
View Related
Nov 10, 2009
I'm trying to better understand loops and variables. I have an instance I'd like to try and implement but I'm struggling with it. I know how to set a variable as 1 and loop through the code increasing the number each time but this one is a little different.
At the start of my code, I scan the worksheets for the worksheet names -
Monday
Tuesday
Wednesday
etc etc
and then assign these worksheets as
Monday = ws
Tuesday = ws2
Wednesday = ws3
etc etc
That way in my code I can do things to the workbooks like -
ws.Range("D4").Value = "blah blah"
and always know it will be tackling the right worksheet regardless of the sheets position in the workbook or sheetcode.
Well I have lots of changes to make on each of these sheets (Monday -> Sunday) which are identical. What I'd like to do is something like this -
For each VARIABLE in (MY VARIABLES WS,WS2,WS3 ETC)
VARIABLE.Range("D4").Value = "blah blah"
Next VARIABLE
View 9 Replies
View Related
Oct 14, 2006
Is it possible to use a variable to change a controls name in a For Loop? I have several worksheet combo boxes that have the same root name, e.g., cbobox1, cbobox2. cbobox3, etc. I want to be able to keep the main name and change the numeric part in a for loop and also perform some function with the control. For example:
For x = 1 To 10
Worksheets("Name").cbobox(x).Clear
Next x
View 6 Replies
View Related
May 7, 2007
Is it possible to do 2 loops in one procedure? I want to copy and paste formula the cells in one column and again in another column so I did two Do Loops. It only ran for the first loop but not on the second, although they have almost the same code (I just edited the column from C to I). Please see below
Dim RowCount As Integer
Sub Format()
Columns("C:C").Select
Selection.Insert Shift:=x1ToRight
Columns("I:I").Select
Selection.Insert Shift:=x1ToRight
Columns("K:L").Select
Selection.Cut
Columns("R:R").Select
ActiveSheet.Paste
Columns("K:L").Select
Selection.Delete Shift:=x1ToLeft
Columns("L:L").Select
Selection.Insert Shift:=x1ToRight
View 5 Replies
View Related