简体   繁体   English

目标搜寻多行-MS-Excel宏

[英]Goal seek multiple rows - MS-Excel Macro

i have 5028 rows excluding the headings and 3 columns, want to set value 1.7 in the cloumn "C" by changing the value in the column "B". 我有5028行(不包括标题和3列),想要通过更改列“ B”中的值在Cloumn“ C”中设置值1.7。

using goal seek option it is possible only for one cell. 使用目标搜寻选项可能仅适用于一个单元。 i want to do the same for 5028 rows, please help to do the task by running some macro. 我想对5028行做同样的事情,请通过运行一些宏来帮助完成任务。

John Bustos pointed to the right idea, here is a working solution: John Bustos指出了正确的想法,这是一个可行的解决方案:

Public Sub Demo()
  Dim rngRow As Range
  For Each rngRow In UsedRange.Rows
    rngRow.Cells(1, 3).GoalSeek Goal:=1.7, ChangingCell:=rngRow.Cells(1, 2)
  Next rngRow
End Sub

Edit: 编辑:

Use ActiveSheet.UsedRange.Rows instead of UsedRange.Rows , if you intend to use this as a Macro in a Modul, not as one of a Worksheet - or any other reference to a valid Range. 如果打算将其用作Modul中的宏,而不用作工作表之一,或使用任何其他对有效范围的引用,请使用ActiveSheet.UsedRange.Rows而不是UsedRange.Rows

For your example, you might prefer to use: Range("A2:C5028").Rows or MySheet.Range("A2:C5028").Rows . 对于您的示例,您可能更喜欢使用: Range("A2:C5028").RowsMySheet.Range("A2:C5028").Rows

Edit: 编辑:

Public Sub Demo()
  On Error Resume Next
  Dim rngRow As Range
  For Each rngRow In ActiveSheet.UsedRange.Rows 
    rngRow.Cells(1, 3).GoalSeek Goal:=1.7, ChangingCell:=rngRow.Cells(1, 2)
  Next rngRow
End Sub

您可以创建一个循环的宏,然后进行远程目标搜索-http: //msdn.microsoft.com/zh-cn/library/office/bb209907%28v=office.12%29.aspx

This is good code. 这是好代码。 I modified it for my purpose, which is for not just a column but multiple columns. 我出于自己的目的对其进行了修改,这不仅适用于一列,而且适用于多列。 In order to run my code, you must first select (highlight) all the cells you wish to GoalSeek (connected like in a table, not separate areas). 为了运行我的代码,您必须首先选择(突出显示)您希望GoalSeek的所有单元格(就像在表中那样连接,而不是分开的区域)。 My code: 我的代码:

Sub GoalSeek_To_0()
' Macro created 15 Jun '18 by Benjamin Cohen

On Error Resume Next

Dim row_, col_ As Range
Dim i_, j_ As Integer
' i_ = 1
  j_ = 1

For Each col_ In Selection.Columns
 For Each row_ In Selection.Rows
  row_.Cells(1, j_).GoalSeek Goal:=0, ChangingCell:=row_.Cells(1, j_).Offset(0, -1)
   row_.Cells(1, j_).Value = 1
 Next row_
  j_ = j_ + 1
Next col_

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM