2017-07-26 137 views
-1

首先发布这里抱歉,如果格式是非常糟糕的。需要一些我正在处理的VBA Excel文件的帮助。代码如下,并在下面的描述。任何帮助,将不胜感激VBA Excel。范围语法和比较器

Sub Main() 

' Declare all variables  
Dim LastRow As Long 
Dim x As Long 
Dim y As Long 
Dim Date1 As Date 
Dim Date2 As Date 
Dim prod As String 
Dim Add As Integer 
Dim var1 As Integer 
Dim var2 As Integer 
Dim ProdString As String 

'Calculate the number of rows in the sheet, then run a for loop for every row. 
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 
For x = 2 To LastRow 
y = x 

'concatenate the current row number (x) and third column to a string (prod string), then check to see what letter is in that cell. 
'Assign a letter to concatenate with variable 'x' to String Prod for the .Range Function 

ProdString = x & 3 
**If Range(ProdString).Value = "I" Then** 
    prod = "K" 
ElseIf Application.ActiveSheet.Range(ProdString).Value = "E" Then 
    prod = "L" 
ElseIf Application.ActiveSheet.Range(ProdString).Value = "O" Then 
    prod = "M" 
ElseIf Application.ActiveSheet.Range(ProdString).Value = "Q" Then 
    prod = "N" 
Else: prod = "O" 
End If 

' Add two numbers together in two different cells and assign to the second cell 
var1 = Application.ActiveSheet.Range("D" & x).Value 
var2 = Application.ActiveSheet.Range("prod" & x).Value 
Add = var1 + var2 
Application.ActiveSheet.Range("prod" & x).Value = Add 


'While Loop to go through applicable date range from starting date to end date and add to those columns 
Do While True 

Date1 = Application.ActiveSheet.Range(y, 6).Value 
Date2 = Application.ActiveSheet.Range(y + 1, 6).Value 
If Application.ActiveSheet.Range(y, 7).Value <> Application.ActiveSheet.Range(y + 1, 7) Then Exit Do 
If Date1 < Date2 Then Exit Do 

var1 = Range("D" & x).Value 
var2 = Range("prod" & y).Value 
Add = var1 + var2 
Range("prod" & y).Value = Add 
y = y + 1 

Loop 

Next x 
End Sub 

我正在使用的Excel文件是一个工作簿,在一张纸上用10k行和15列。我试图在一列中添加一个数字到另一个基于日期。但目前我遇到的问题是试图找出Bolded.Range函数以及为什么它不起作用。我试着在特定的工作簿和页面名称前面进行尝试,但是当我这样做时,我会遇到全局范围错误。我只想比较指定范围内单元格内的内容,并且如果它是某个字符串,则将值赋给不同的字符串值。 谢谢。

+1

您正在查看PodString,它是一个字符串,同时尝试将其用作范围。你将需要定义你想要工作的范围。例如,如果PodString = fdsa,那么Range(“fdsa”)会出错;如果PodString是范围和=范围(“A2:B2”),则范围(PodString)=范围(“A2:B2”)。 – Cyril

+1

另外,当你开始你的子,你声明'X',但从未给它一个值。为什么甚至使用'x'? 'Range(podString)'是什么意思?这将是一个单一的数字,不是吗?像'范围(3)'? – BruceWayne

+1

@BruceWayne我有一个更重要的问题列表,希望得到一些代码后,意图更清晰。这是......粗略地推断变量的意图。 – Cyril

回答

0

规划与代码更新,一旦我能搞清楚发生了什么事......


试图建立在我对你的评论,用什么,我觉得我可以从你的代码解释。

我相信你正试图循环遍历一个范围,尽管我试图根据它们的使用位置来放置x和y变量。我会尝试推断......假设您通过ProdString = x & 3检查意味着第三行,x是您的专栏。我看到了I3(Cells(3,9)),E3(Cells(3,5)),O3(Cells(3,15))和Q3(Cells(3,17))中特定字符串的比较。

我没有看到ProdString应该是什么......你在比较什么?

我想像你想要的东西喜欢(这是不够的代码ATM):

Dim ProdString as String, prod as String, x as Integer 

ProdString = Cells(3,prod).Value 

If Cells(3,9).Value=ProdString Then 
    prod = Cells(3,11).Value 'Getting lost at why prod us being used as such 
    Else 
    End If 

我无法弄清楚你想添加什么,由于列= X,但你使用范围以后,如范围(“D”& x)。


编辑:

得到一些澄清我的问题后,我将有以下更改你的代码(仅在我需要纠正的范围错误的商品带来:

Dim prod as String, x as Integer 

For x = 2 to LastRow 

    If Cells(x,3).Value = "I" Then 
     prod = "K" 
     Else 'Only showing the one option to shorten my response 
     End If 

Next x 

请注意,ProdString被删除,因为没有任何值,字符串x & 3是而不是一个范围,并且不能用作范围,例如Range(ProdString)。如果要循环遍历行的列,你可以忽略使用Range()并通过Cells()去做一个简单的方法。

针对此代码中的内容,您说的是在循环单元中保存的值具有值“I”。在没有看到数据的情况下,我只能假设你实际上只是在寻找价值“我”,尽管使用prod让我觉得这是错误的。我怀疑你正在尝试捕获列名/数字,所以你需要看看,因为如果你被锁定到第3列并循环行,你永远不会看到列“我”。我建议以下变化更换督促

Dim x as Integer, prod as Integer 

For x = 2 to LastRow 

    If Cells(x,3).Value = "I" Then 'Ensure that this is truly supposed to be a value of I in the cell 
     prod=Cells(x,11).Value 
     Cells(x,11).Formula=Cells(x,4).Value+prod 'It looks like you're overwriting the cell in row x, column 11 
     Else 
     End If 

Next x 

让我知道这是否清除任何东西。

+0

感谢您花时间查看我的代码。我急忙把它提出来,意识到我已经拿出了一些重要的部分。代码的要点是为每一行做一个for循环,根据另一个值将一行中的值添加到同一行的另一列。然后做一个while循环,并不断将x行中的数字添加到它下面的后续y行。如果帐户不匹配或日期不同步,请退出while循环并继续使用x for循环。希望能澄清一些混乱。我主要关心的是获得.Range函数的工作。 – JGenchi

+0

@JGenchi然后,最初的评论将立场(对你的文章)。您没有将ProdString限定为范围,并且它不是您正在使用的范围。我会更新我的答案以表明我的意思。 – Cyril

+0

@JGenchi请参阅下面的编辑...我已更正您的范围问题,但如上所述,您可能遇到更多问题与您选择的方法。 – Cyril