Changing Macro To Use A Loop

Mar 12, 2014

I'm using a macro to drag down a formula across a worksheet then again further down the worksheet which is working ok but I know there has to be a better way of doing it. At the moment every time I add a new column I have to edit the macro over and over and its getting out of hand. I know there must be a way of rewriting the macro into a loop but my skills are obviously still new.

Here's the macro:

[Code] ......

This continues over to column AO so far then I drop down a few rows and do it again:

[Code] .....

I think I need to set variables that set the row and column each time. maybe a "drag formula one column at a time from row x to row y until column header is empty then move to the new row and repeat"?

View 4 Replies


ADVERTISEMENT

Avoid Changing A Loop Counter Within A Loop?

Oct 24, 2009

I've worked on a solution for this thread (http://www.excelforum.com/excel-prog...-automate.html) but have been mentally challenged with how to avoid changing the loop counter in one of the loops I have used to resort an array of file names from the getopenfile dialog.

The aim of the shown code (see post 12 of the above link for attached file) is to check if the file containing the macro is included in the array returned by getopenfile while sorting the array of file names, and if so, moving it to the end of the array for "deletion" by redimming the array to exclude the last item. This problem of the open file being selected in the dialog may never arise, but... as the OP's request in the other thread was to allow two-way comparisons between numerous files, I've considered it likely enough to test for.

Here's the code I have settled for esp between the commented lines of hash symbols, which does change the counter (see the commented exclamation marks), but prevents an infinite loop (on my second try!) by using a second boolean flag of "HasCounterBeenChanged". Is there a better way of doing this? Or, alternatively (not in my thread title), is it possible to prevent the active file being selected through one of the arguments in the getopenfilename method?

View 3 Replies View Related

Loop With Changing Variables

Oct 16, 2007

i have set up some test script below!

i dont know if it is possible but can you concatinate two variables i.e in the example below can i move from Sum1 to Sum6 using the intiger stored in i as the end of the sum variable

Sub Sum()
Dim i As Integer
Dim Sum1, Sum2, Sum3, Sum4, Sum5, Sum6 As Integer
Dim Ltr As String
Dim Sum As String
Sum = "Sum"

Sum1 = Range("B5").Value
Sum2 = Range("C5").Value
Sum3 = Range("D5").Value
Sum4 = Range("E5").Value
Sum5 = Range("F5").Value
Sum6 = Range("G5").Value

For i = 1 To 6
MsgBox "The value in cell " & i "is " & Sum & i
Next i

End Sub

View 9 Replies View Related

VBA: How To Program A Changing Variable Name In A Loop

May 20, 2006

Trying to code my first custom VBA function. The function is supposed to translate the words in a cell by repeatedly looping through the string and looking through a list of provided translation words.

Problem is the variable name changes with each loop, and I can't figure out how to program the name change.

Function TestTranslate(TranslateCell, ProductCell) As String

Dim t As Long
Dim TranslateCeiling As Long
Dim SS1 As String
Dim SS As String
Dim Q, R As String
Dim W As String
Dim LLRString, MLRString, SLRString As String
Dim LRString, MRString, SRString As String
Dim TArray() As String

Looks like the loop keeps defining Q as "SS" & t instead of doing the select case formula. I tried removing the Q variable altogether and making it read just "SS" & t = Replace(R, LLRString, LRString) for example and keep getting a compile error.

View 9 Replies View Related

For Loop For Changing Formulae Based On Other Sheet

Jul 27, 2006

I want to write a For loop so that it checks the range G2:R2 of Sheet1 for the first non-zero cell in that range. When it comes to the first non-zero cell, it will change the formula of A2 on Sheet2 to have the SUM of that non-zero cell and the next two cells to the right. Example:

SHEET1-
G2 = ""
H2 = ""
I2 = ""
J2 = "3"
K2 = "4"
L2 = "8"
M2 = "9"
N2 = "2"
O2 = "5"
P2 = "3"
Q2 = "11"
R2 = "7"

SHEET2-
A2 = SUM('Sheet1'!J2:L2) --->"15"

View 5 Replies View Related

Changing UDF To Macro

Feb 8, 2013

I found the following function in an old thread.

Public Function cleanString(fNameStr As String)Dim i As IntegerConst BadChars = "=+/'*?).,#%~!($[]÷™©" For i = 1 To Len(BadChars) fNameStr = Application.WorksheetFunction.Substitute(fNameStr, _ Mid(BadChars, i, 1), "") Next i cleanString = fNameStr
End Function

How would this be modified to work as a macro on selected cells (with selection?) I lost the formatting

View 3 Replies View Related

Changing A Macro

Jan 6, 2009

what needs to be changed on this code to only apply once for the row and not over and over again.

Dim Cell As Range
If Application.Intersect(Target, Range("ActivityRange")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each Cell In Range("ActivityRange")
If Len(Cell.Value) > 0 Then Cell.Value = Cell.Value & Chr(10)
Next Cell
Application.EnableEvents = True

View 9 Replies View Related

Using Same Macro To Sum Changing Ranges

Jul 21, 2014

Is it possible to use the same macro when ranges have been changed? Attached is a sheet that I update frequently (rows will change day to day, not columns) and the goal is to have a dynamic macro that sums the rows highlighted in yellow and red. Is this possible?,

View 4 Replies View Related

VBA Macro (Changing Cells From A1-C3 Then A2-C2)

Aug 7, 2014

What my code needs to do: It needs to verify data in column rows say for this example columns A to C and infinite rows. It needs to verify the data in this order A1, B1, C1, A2, B2, C3 etc. (One of the part I have problems to do) It also needs to add to a row/cell "1" when certain conditions. (In the same line as the last checked data in the column "D" say for exemple last data that was checked is "A3" well the 1 must be written in "D3". The conditions are simple the first Data the macro encounters must be higher than 0.25 and all following data must be higher than 6, whenever the next data is lower than 6 or empty add 1 to the corresponding D row and restart at the 0.25 cell "checking".

View 2 Replies View Related

Run Macro If Values Are Changing?

Dec 21, 2011

I have a code that clears the content of a cell if the value of that cell exceeds two.

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Cells(1, 1).Value > 2 Then Cells(1, 1).ClearContents
End Sub

My problem is the value of this cell comes from a function and changes every second automatically (linked to a data feed). So I'm not changing it manually, therefore my code is not working unless I touch it. I just wanted to ask if I can add a code onto this which checks the values and runs the ClearContents macro if there is any change.. I've got a few sheets like this so I need this to run in the background..

View 2 Replies View Related

Changing VBA Macro Using InStr?

Aug 29, 2012

I want a vba macro which performs some thing like this. I think probably this can be done by using InStr function, the macro attached here just find the exact same value and shows the result but i want similar values,

Code:
Sub a()
lastrow = 10
For arow = 11 To 16

[Code].....

View 7 Replies View Related

Changing Ranges In A Macro?

Jun 24, 2013

Code:

Sub appointment_nieuw()
With CreateObject("Outlook.Application").CreateItem(1)
.Subject = Range("C3")
.Start = DateValue(Range("A3")) + TimeValue(Range("B3"))
.Duration = 0
.Location = Range("D3")
Dim cell As Range
For Each cell In Range("F2:H2")
.body = .body & Space(2) & cell
Next
.Save
End With
End Sub

I have this macro that works great, but every time I have a different appointment. I have to go into VB editor and change the ranges for the subject, datevalue, timevalue, location and body.

Can I get a text box (or something better) to pop up and I enter the ranges for the 5 in it and have it change in the macro?

View 6 Replies View Related

Macro Not Changing Cells

Dec 20, 2006

I have button in the Excel sheet.When I click the button the following code got triggered.

Sub Button1_Click() 'YieldCurveGet
Dim arrayData As String
Dim CurveString As String
'arrayData = "Test,Test1....." '(for eg)
arrayData = WorksheetFunction.RTD("OrcExcel.RTDServer", "", "YieldCurveGet", "AUD yield curve", "AUD", Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null)
CurveString = Split(arrayData, ",")
i = -1
For x = 1 To (UBound(CurveString) / 2)
i = i + 1
range("C" & x) = CurveString(i)
i = i + 1
range("D" & x) = CurveString(i)
Next x
End Sub

After triggered get invoked I need to set value of "arrayData " from VBA to Excel.Iam not sure what's going wrong.I cannot see the value in the Excel.

View 8 Replies View Related

How To Create A Word Changing Macro

May 22, 2014

Is there any way to create a Macro that is any cell in column D has the word Shop it would change the adjacent word in column E from Roof Time to Shop Time?

View 1 Replies View Related

Name Manager Changing Range After Run Macro

Mar 6, 2014

I have a workbook that has a saved range. I created it with the Name Manager. See below:

[Code]....

When it looks like that the VBA works great and creates my pivots. But after I run my first macro to mess format my source data the range ends up looking like below:

[Code] .......

It is for some reason changing the "A" to "Y".

How I can get it to stay with the original data?

View 2 Replies View Related

Runing A Macro On By Changing A Value Of The Cell

Sep 11, 2008

Is there a way to change the value of cell A1, for example, from one value to another, hit the enter key, and activate a macro this way.

A couple of qualifications:
- the value in cell A1 will never be the same (that is it could be -23.43 or .12)
- I can not simply tie the macro to the "enter" key as there will be numerous cells (A1, B3, C90 and so on) in the same sheet that I will need to do this with, each tied to a different macro.

View 14 Replies View Related

Changing Macro To Custom Function

Nov 19, 2008

I have a calendar I have developed that highlights the current date and opens by default to the current month. Each worksheet is a seperate month. The current date highlighting is written with a public macro and the opening to current month is a private macro. The problem comes when this workbook is shared and the other users have to agree to accept my macro because of security. I would like to bypass that requirement and just have the workbook stand alone without the permission.

View 5 Replies View Related

Macro With Changing Column Numbers

Jun 14, 2009

Is it possible for excel to monitor Row J for a letter "Y". If a "Y" is put into a cell in row J a macro runs. If the "Y" was in column 6 I need the macro to copy A6 B6 C6 D6 E6 F6 etc to other cells on a different worksheet.If "Y" was in column 7 it will copy A7 B7 C7 etc to the same other cells on a different worksheet.

View 14 Replies View Related

Changing Date Format Macro

Jun 17, 2009

I would need a macro what would change this format (15 Jun 2009) in cell B2 to 2009.06.15.

View 5 Replies View Related

Macro For Changing Info In Formulas

Nov 20, 2009

I have a template work book that has several worksheets with numerous formulas thorughout.

For example;
=SUMPRODUCT(--ISNUMBER(SEARCH("MCSH",P9:P218)),--ISNUMBER(SEARCH("HWD",G9:G218))).

When I set up another template for a different crew I manually have to go through the entire work book and change the crew identifier (MCSH) in every formula.

View 4 Replies View Related

Macro For Changing Colors In Cells?

Mar 13, 2012

I have a worksheet where I want the to change the collor of a range of cells in accordance to the value in another range of cells.

So if the value is "Gron" in cells C4:C27 then I want cells J4:J27 to return the color green. If, however, the value is "Rod" in cells C4:C27 then I want cells J4:J27 to return the color red.

View 9 Replies View Related

Changing Cost Allocation Macro?

May 23, 2012

I am trying to write a Macro to allocate a certain cost across thousands of orders. The number of orders will obviously change monthly. The cost is relatively small, and when divided evenly by the total orders needs to be rounded up to the nearest cent (Typically $.01 - $.02)

I then need the cost to be insered in a colum running next to a total cost column, but I need it to stop when it reaches the total cost and just start adding zeros to the rest of the orders so as not to overcharge.

Not sure if I need to incorporate solver somehow. I figured I would reference the allocated cost from a cell in the worksheet but not sure how to insert any algebraic logic into a macro.

View 9 Replies View Related

Macro To Prevent Changing The Value Within A Cell

Jan 11, 2007

I would like to write a macro that performs the following function:

I have a value in cell A1

In cell A2, I have the formula "=A1"

I want to make it so that cell A2 can never be directly changed; the only way to change the value in A2 is by changing A1. A2 should always equal A1.

I want a message box to pop up and alert the user whenever they attempt to modify A2, instructing them that if they wish to change A2, they have to change A1.

I had written this macro, and it kinda does the trick:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)


If Range("a2") Range("a1") Then
MsgBox "text goes here"
Range("a2").Value = Range("a1")
End If

End Sub

When I attempt to change cell A2, I get the MsgBox popup and it works how I want it to. However, when I alter cell A1, I get the same popup before cell A2 updates (to be precise, the popup occurs after I click another cell). I would like this to be immediate and without a MsgBox.

View 9 Replies View Related

Event Macro Changing Sheet Name

May 23, 2009

I have a worksheet called "Roster" with a list of names from D7:D17 each person on the list has a separate sheet that is called by their name. the sheets are not linked up. the names were just manually typed into the list and into each sheet name.

If any name is changed on the list on "Roster!D7:D17" i would like the sheet that has that name to change as well.

This is the code that i was sent, but i do not know enough about VBA code to understand exactly what is happening, thus i cannot diagnose the problem. I placed the following code into the page "Roster" (by right-clicking and then pasting the code) then i closed, but nothing happens. I can mess with the code and get an error message. but that is about it. error messages usually say something about the debugger, or runtime error or something.

there is the
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
OldVal(0) = OldVal(1)
OldVal(1) = Target(1).Value
End Sub

View 9 Replies View Related

Macro Security Changing Worksheet

May 13, 2006

I have created a workbook. That has 35 worksheets within it. I have created a series of buttons and put in a macro code so that when the user clicks on the button it changes the active worksheet to what they clicked on.

So basically when they click button b it changes to worksheet b and if they click button c it changes to worksheet c, ETC.

The problem I have is that since it is a macro it works fine on my computer but when it is sent to other users, because of security settings on their pcs it will disable the macro [thus the buttons will not work or show up]. I have created a digital trust certificate for it. However it is alot of steps to install this for the basic user. Is there a way to create a button that will do what I want without using a macro code. In that it will be able to go to many different users and wont have to worry about the buttons not working because of security settings?

View 9 Replies View Related

Add Formula Via Macro On Changing Range

Oct 25, 2006

I’m wondering if it possible to have a variable in a Formula. I was looking around, but non of the other examples I found was working. The code I was trying is:

Sub All()
Dim lngDataRows As Long
Dim lngDataRowsSum As Long
' add sum
lngDataRowsSum = Range("A5").End(xlDown).Row
Range("A" & lngDataRowsSum).Offset(1, 0).FormulaR1C1 = "Sum"
Range("M" & lngDataRowsSum).Offset(1, 0).FormulaR1C1 = "=sum(M6:M & lngdatarowssum)"
End Sub

Another code I tried was from this thread

Sub All()
Dim lngRowsBottom As Long
Dim lngRowsTop As Long
' add sum
lngDataRowsSum = Range("A5").End(xlDown).Row
Range("C10").formula = "=sum(R["&lngrowstop&"]C:R["&lngrowsbottom&"]C)"
End Sub

The error message is: “Compile error: Expected: enf of statement” and the part “[C:R[“ is highlighted. Tried various ways to solve it but nothing was working.
Does anyone of you know how to get it up and running?

View 3 Replies View Related

Changing Checkbox Value Without Re-running Macro

Apr 5, 2007

I'm trying to do what I thoughts was the simpliest thing, but for some reason can not manage.

I try to change to value of a checkbox, by a code, without re-running the CheckBox_Click () Macro. I have put a checkbox in a worksheet, and when tickmarking it, a msgbox will pop up (one msgbox for tickmark on), and you can press Yes or No. Pressing No, I will sett the Checkbox.value = False. The problem is, when I use this command, the checkbox_click macro will run all over again. Now, the checkbox is False, and antother msgbox will pop up (another msgbox for tickmark off). The results is then that I first get the msgbox I want, but it is followed by the msgbox for tickmark off, which should only pop up when the checkbox is ticked off manually.

Code is something like follows:

Private Sub CheckBox1_Click()
Dim mySheet As Worksheet

Set mySheet = ActiveSheet

Application. ScreenUpdating = False

View 3 Replies View Related

Automatically Run Macro On Cell Changing

Jan 24, 2008

I am writing/creating macros and I want to create one that will automatically run when a cell value is changed to a certain entry.

View 2 Replies View Related

Loop Macro ...

Apr 15, 2009

I found this script which is very similar to what I want to do but I am not sure how to finish modifying it.

This script compares every cell in Column B to Z on Sheet1 with Cell A1 on Sheet2, if it matches it will copy the active cell in column A and past the results to column A no Sheet3.

View 10 Replies View Related

Loop If Then Macro

May 12, 2012

I am the consolidator for a specfic Excel file to be converted into CSV. In order for this file to be uploaded into our database properly it needs to be in a specific format and certain values must be entered if other cells have been filled in. At this point I have locked the formating so users cannot change it and I have created a Macro for blank cell rules.

The Macro is to verify that cells are not blank if the value in A is not blank. At this point it works as it should, for row 4. However, I would like to Loop this Macro throughout the entire worksheet.

The Macro I have created is (minus several columns for simpler reading purposes):

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Worksheets("JE FILE").Range("B4").Value = "" And Worksheets("JE FILE").Range("A4").Value "" Then
MsgBox "You must fill in Customer."
Cancel = True

[Code] .......

How do I LOOP the Macro?

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved