我试图找到A2到A999内的单元格的最大值。我试图做一个For循环来遍历每一个,但是找到最大rcell的逻辑有问题。我希望有一个内置的rcell.max功能?
Set rng = loopset.range("A2-A999")
For Each rcell in rrng.cells
'Find the max rcell
Next rcell
答案 0 :(得分:10)
Application.worksheetfunction.max(range("a:a"))
会为你做这件事
答案 1 :(得分:3)
您确实可以使用函数application.worksheetfunction.max(rng),如上所述。为了给出更完整的答案,几乎任何工作表上可用的Excel公式都可以通过application.worksheetfunction集合在VBA代码窗口中使用。这包括max,min,sumif,vlookup等。
这应该为您提供与在工作表上使用该函数时所执行的函数中涉及的参数相同的屏幕描述。但是,正如另一个用途所指出的那样,使用application.max(range)并没有给出相同的参数帮助。同样的用户注意到,使用application.max(rng)和worksheetfunction.max(rng)之间的错误处理有所不同,你可以在下面的评论中看到这一点
对于从值列表中确定最大值的编程逻辑,基本逻辑是:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "queues.h"
void printState(State s){
printf("\tsg1\t\tsg2\n");
printf("\t%d\t\t%d\n", s.sg1, s.sg2);
printf("\t\tt=%d\n", s.time);
printf("------------------------------------------\n");
}
/* The function action generates a new state from an existing state.
*/
State action(State s, int actionType, int cap1, int cap2 ) {
State res;
res.time = s.time;
res.sg1 = s.sg1; /* sg1 & sg2 are original values/previous state */
res.sg2 = s.sg2;
if (res.sg1 <= res.sg2 ){ /* A is smaller or equal to B */
if (res.sg1 != 0) {
res.sg2 -= res.sg1 ;
res.time += res.sg1 ;
res.sg1 = 0;
} else {
res.time +=res.sg2;
res.sg2 = 0;
}
//printf("next time point A<B: time=%d res.sg1=%d res.sg2=%d\n\n", res.time, res.sg1, res.sg2);
} else { /* A is bigger than B */
if (res.sg2 != 0) {
res.sg1 -= res.sg2;
res.time += res.sg2;
res.sg2 = 0;
} else {
res.time += res.sg1;
res.sg1 = 0;
}
//printf("next time point A>B: time=%d res.sg1=%d res.sg2=%d\n\n", res.time, res.sg1, res.sg2);
}
switch(actionType){
case 1: /* flip A */
res.sg1 = cap1 - res.sg1;
break;
case 2: /* flip B */
res.sg2 = cap2 - res.sg2;
break;
case 3: /* flip A&B */
res.sg1 = cap1 - res.sg1;
res.sg2 = cap2 - res.sg2;
break;
case 4: /* do nothing */
res.sg1 = res.sg1;
res.sg2 = res.sg2;
break;
}
//printf("state after action %d:\n", actionType);
//printState(res);
return res;
}
int compare(State a, State b){ /*Return -1 if a < b, 0 if a==b, +1 if a > b*/
if(a.time < b.time){
return -1;
}
if(a.time > b.time){
return 1;
}
if(a.sg1 < b.sg1){
return -1;
}
if(a.sg1 > b.sg1){
return 1;
}
if(a.sg2 < b.sg2){
return -1;
}
if(a.sg2 > b.sg2){
return 1;
}
return 0;
}
int findPos(State s, List li){
int pos = 0;
/*
if(isEmptyList(li)){
//printf("List Empty\n");
return pos;
}
*/
while(li != NULL){
int com = compare(li->item, s);
if (com == 0){
return -1;
}
if (com == 1){
return pos;
}
pos+=1;
li=li->next;
}
return pos;
}
void insertUnique(State s, Queue *q){
int pos = findPos(s, q->list);
//printf("Inserting new state at pos %d\n", pos);
//printState(s);
if(pos == 0){
enqueue(s, q);
}else if(pos != -1){ /*-1 is value for duplicate*/
addItemAtPos(q->list, s, pos);
//listPrinter(q->list);
}
if(pos == -1){
//printf("DENIED A STATE FOR DUPLICATE\n");
}
}
int isCorrect(State act, int goalTime){
if(act.time == goalTime ) return 1;
return 0;
}
int isValidAction(State act, State curr, int action, int goalTime){
if(act.time > goalTime) return 0;
if(curr.sg1==0 && curr.sg2 == 0 && action == 4){
return 0;
}
return 1;
}
/* The function timeable checks whether a given time can be determined
* exactly by two sandglasses with given capacities.
*/
int timeable(int cap1, int cap2, int goalTime ) {
/* Should probably make a queue of lists, where each list in the queue is a list of possible actions with the following state */
/*Queue q = newEmptyQueue();*/ /* This mgiht work */
if(goalTime == 0){
return 1;
}
State begin; /* Maybe change to global but idk */
begin.time = 0;
begin.sg1 = cap1;
begin.sg2 = cap2;
//printf("begin:\n");
//printState(begin);
Queue q = newEmptyQueue();
enqueue(begin, &q); /*Enqueue begin because less code dupe*/
//free(begin);
while(1){ /*No end for while because there SHOULD always be a return*/
if(isEmptyQueue(q)){
//printf("QUEUE IS EMPTY\n");
freeQueue(q);
return 0;
}
State curr = dequeue(&q);
for(int i=1; i<=4; i++){
State act = action(curr, i, cap1, cap2);
if (isCorrect(act, goalTime)){ /**TODO**/
freeQueue(q);
return 1;
}
if(isValidAction(act, curr, i, goalTime)){ /**TODO**//*Anything larger than queue isn't worth computing*/
//printf("ENQUEUEING FOR ACT %d\n", i);
//printState(act);
insertUnique(act, &q); /** Is action working?**/
}
}
}
}
/* main performs a dialogue with the user. The user is asked to provide three numbers:
* the (positive) capacities of the sandglasses and the goal time (>= 0).
* The program indicates whether the goal time can be determined exactly.
* The dialogue is ended by the input '0'.
*/
int main(int argc, char *argv[]){
int cap1, cap2, goalTime;
printf("give the sandglass capacities and the goal time: ");
scanf("%d",&cap1);
while ( cap1 > 0 ) {
scanf("%d",&cap2);
assert( cap2 > 0 );
scanf("%d",&goalTime);
assert( goalTime >= 0 );
if ( timeable(cap1, cap2, goalTime) ) {
printf("%d and %d can time %d\n", cap1, cap2, goalTime);
} else {
printf("%d and %d cannot time %d\n", cap1, cap2, goalTime);
}
printf("\ngive the sandglass capacities and the goal time: ");
scanf("%d",&cap1);
}
printf("good bye\n");
return 0;
}